/
LoginFormModel.cs
613 lines (529 loc) · 22 KB
/
LoginFormModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using Microsoft.Owin.Security;
using ServiceStack.Text;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Frontend.Mvc.Helpers;
using Telerik.Sitefinity.Mvc.Proxy;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Claims;
using Telerik.Sitefinity.Security.Configuration;
using Telerik.Sitefinity.Security.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web;
using Config = Telerik.Sitefinity.Configuration.Config;
using SecConfig = Telerik.Sitefinity.Security.Configuration;
namespace Telerik.Sitefinity.Frontend.Identity.Mvc.Models.LoginForm
{
/// <summary>
/// This class is used as a model for the <see cref="LoginFormController"/>.
/// </summary>
public class LoginFormModel : ILoginFormModel
{
#region Properties
/// <inheritDoc/>
public string ServiceUrl
{
get
{
this.serviceUrl = this.serviceUrl ?? this.GetClaimsIssuer();
return this.serviceUrl;
}
set
{
this.serviceUrl = value;
}
}
/// <inheritDoc/>
public bool AllowResetPassword { get; set; }
/// <inheritDoc/>
public bool ShowRememberMe { get; set; }
/// <inheritDoc/>
public string MembershipProvider
{
get
{
if (string.IsNullOrWhiteSpace(this.membershipProvider))
{
var provider = UserManager.GetDefaultProviderName();
var availableProviders = UserManager.GetManager().GetContextProviders();
if (!availableProviders.Any(x => x.Name == provider))
{
provider = availableProviders.First().Name;
}
this.membershipProvider = provider;
}
return this.membershipProvider;
}
set
{
this.membershipProvider = value;
}
}
/// <inheritdoc />
public string CssClass { get; set; }
/// <inheritDoc/>
public Guid? LoginRedirectPageId { get; set; }
/// <inheritDoc/>
public Guid? RegisterRedirectPageId { get; set; }
/// <inheritDoc/>
public string SerializedExternalProviders
{
get
{
return this.serializedExternalProviders;
}
set
{
if (this.serializedExternalProviders != value)
{
this.serializedExternalProviders = value;
}
}
}
/// <summary>
/// Gets a value indicating whether password retrieval is enabled.
/// </summary>
/// <value>
/// <c>true</c> if password retrieval is enabled; otherwise, <c>false</c>.
/// </value>
public virtual bool EnablePasswordRetrieval
{
get
{
return UserManager.GetManager(this.MembershipProvider).EnablePasswordRetrieval;
}
}
/// <summary>
/// Gets a value indicating whether password reset is enabled.
/// </summary>
/// <value>
/// <c>true</c> if password reset is enabled; otherwise, <c>false</c>.
/// </value>
public virtual bool EnablePasswordReset
{
get
{
return UserManager.GetManager(this.MembershipProvider).EnablePasswordReset;
}
}
/// <summary>
/// Gets a value indicating whether the SMTP settings are set.
/// </summary>
/// <value>
/// <c>true</c> if SMTP settings are set; otherwise, <c>false</c>.
/// </value>
public virtual bool AreSmtpSettingsSet
{
get
{
return LoginUtils.AreSmtpSettingsSet;
}
}
#endregion
#region Public Methods
/// <inheritDoc/>
public virtual LoginFormViewModel GetLoginFormViewModel()
{
var viewModel = new LoginFormViewModel();
this.InitializeLoginViewModel(viewModel);
return viewModel;
}
/// <inheritDoc/>
public virtual void InitializeLoginViewModel(LoginFormViewModel viewModel)
{
if (viewModel != null)
{
viewModel.ServiceUrl = this.ServiceUrl;
viewModel.MembershipProvider = this.MembershipProvider;
viewModel.RedirectUrlAfterLogin = this.GetPageUrl(this.LoginRedirectPageId);
viewModel.RegisterPageUrl = this.GetPageUrl(this.RegisterRedirectPageId);
viewModel.ShowRegistrationLink = this.RegisterRedirectPageId.HasValue;
viewModel.ShowForgotPasswordLink = this.AllowResetPassword && (this.EnablePasswordReset || this.EnablePasswordRetrieval) && this.AreSmtpSettingsSet;
viewModel.Realm = ClaimsManager.CurrentAuthenticationModule.GetRealm();
viewModel.CssClass = this.CssClass;
viewModel.ShowRememberMe = this.ShowRememberMe;
if (!string.IsNullOrEmpty(this.serializedExternalProviders))
{
var externalProviders = JsonSerializer.DeserializeFromString<Dictionary<string, string>>(this.serializedExternalProviders);
var availableProviders = ClaimsManager.CurrentAuthenticationModule.ExternalAuthenticationProviders.Where(x => x.Enabled == true && !string.IsNullOrEmpty(x.Title)).Select(x => x.Title).ToList();
var filteredExternalProviders = externalProviders.Where(x => availableProviders.Contains(x.Key));
viewModel.ExternalProviders = new Dictionary<string, string>();
foreach (var kv in filteredExternalProviders)
{
viewModel.ExternalProviders.Add(kv.Key, kv.Value);
}
}
}
}
/// <inheritDoc/>
public virtual ResetPasswordViewModel GetResetPasswordViewModel(string securityToken, bool resetComplete = false, string error = null)
{
var securityParams = HttpUtility.ParseQueryString(securityToken);
var userId = this.GetUserId(securityParams);
var userManager = UserManager.GetManager(this.MembershipProvider);
string question = null;
if (userId != Guid.Empty)
{
var user = userManager.GetUser(userId);
if (user != null)
{
question = user.PasswordQuestion;
}
}
return new ResetPasswordViewModel()
{
CssClass = this.CssClass,
LoginPageUrl = this.GetPageUrl(null),
RequiresQuestionAndAnswer = userManager.RequiresQuestionAndAnswer,
Error = error,
ResetPasswordQuestion = question,
ResetComplete = resetComplete,
SecurityToken = securityToken
};
}
/// <inheritDoc/>
public virtual ForgotPasswordViewModel GetForgotPasswordViewModel(string email = null, bool emailSent = false, string error = null)
{
return new ForgotPasswordViewModel()
{
CssClass = this.CssClass,
LoginPageUrl = this.GetPageUrl(null),
EmailSent = emailSent,
Error = error,
Email = email
};
}
/// <inheritDoc/>
public virtual void ResetUserPassword(string newPassword, string answer, NameValueCollection securityParams)
{
var userId = this.GetUserId(securityParams);
if (userId == Guid.Empty)
{
throw new ResetPasswordUserNotFoundException();
}
var manager = UserManager.GetManager(this.MembershipProvider);
using (new ElevatedModeRegion(manager))
{
var resetPassword = manager.ResetPassword(userId, answer);
manager.ChangePassword(userId, resetPassword, newPassword);
manager.SaveChanges();
}
SecurityManager.GetManager().ExpireResetPasswordToken(securityParams);
}
/// <inheritDoc/>
public virtual ForgotPasswordViewModel SendResetPasswordEmail(string email)
{
var viewModel = new ForgotPasswordViewModel();
viewModel.Email = email;
viewModel.EmailSent = false;
var manager = UserManager.GetManager(this.MembershipProvider);
var user = manager.GetUserByEmail(email);
if (user != null)
{
if (UserManager.ShouldSendPasswordEmail(user, manager.Provider.GetType()))
{
var currentNode = SiteMapBase.GetActualCurrentNode();
if (currentNode != null)
{
var resetPassUrl = Url.Combine(currentNode.Url, "resetpassword");
try
{
UserManager.SendRecoveryPasswordMail(UserManager.GetManager(user.ProviderName), email, resetPassUrl);
}
catch (Exception ex)
{
if (Exceptions.HandleException(ex, ExceptionPolicyName.IgnoreExceptions))
throw ex;
viewModel.Error = ex.Message;
}
}
}
}
// Always send correct message (for security)
viewModel.EmailSent = true;
return viewModel;
}
/// <inheritDoc/>
public virtual string GetErrorFromViewModel(System.Web.Mvc.ModelStateDictionary modelStateDict)
{
var firstErrorValue = modelStateDict.Values.FirstOrDefault(v => v.Errors.Any());
if (firstErrorValue != null)
{
var firstError = firstErrorValue.Errors.FirstOrDefault();
if (firstError != null)
{
return firstError.ErrorMessage;
}
}
return null;
}
/// <inheritDoc/>
public virtual string GetPageUrl(Guid? pageId)
{
if (pageId.HasValue)
{
return HyperLinkHelpers.GetFullPageUrl(pageId.Value);
}
else
{
var currentNode = SiteMapBase.GetActualCurrentNode();
return currentNode != null ? HyperLinkHelpers.GetFullPageUrl(currentNode.Id) : null;
}
}
public virtual LoginFormViewModel Authenticate(LoginFormViewModel input, HttpContextBase context)
{
input.LoginError = false;
string errorRedirectUrl = GetErrorRedirectUrl(context);
var redirectUrl = this.GetReturnURL(context);
User user;
UserLoggingReason result = SecurityManager.AuthenticateUser(
this.MembershipProvider,
input.UserName,
input.Password,
input.RememberMe,
out user);
if (result != UserLoggingReason.Success)
{
errorRedirectUrl = AddErrorParameterToQuery(errorRedirectUrl);
SFClaimsAuthenticationManager.ProcessRejectedUserForDefaultClaimsLogin(context, result, user, input.RememberMe, redirectUrl, errorRedirectUrl);
input.LoginError = true;
}
else
{
redirectUrl = RemoveErrorParameterFromQuery(redirectUrl);
input.RedirectUrlAfterLogin = redirectUrl;
SystemManager.CurrentHttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { RedirectUri = redirectUrl });
}
return input;
}
private static string AddErrorParameterToQuery(string redirectUrl)
{
var uriBuilder = new UriBuilder(redirectUrl);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["err"] = true.ToString();
uriBuilder.Query = query.ToString();
redirectUrl = uriBuilder.ToString();
return redirectUrl;
}
private static string RemoveErrorParameterFromQuery(string redirectUrl)
{
var uriBuilder = new UriBuilder(redirectUrl);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
if (query.Keys.Contains("err"))
{
query.Remove("err");
uriBuilder.Query = query.ToString();
redirectUrl = uriBuilder.ToString();
}
return redirectUrl;
}
private static string GetErrorRedirectUrl(HttpContextBase context)
{
string errorRedirectUrl;
if (context.Request.UrlReferrer?.AbsoluteUri != null)
{
errorRedirectUrl = context.Request.UrlReferrer.AbsoluteUri;
var param = context.Request.Params[MvcControllerProxy.ControllerKey];
if (param != null)
{
var uriBuilder = new UriBuilder(errorRedirectUrl);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[LoginControllerKey] = param;
uriBuilder.Query = query.ToString();
errorRedirectUrl = uriBuilder.ToString();
}
}
else
{
errorRedirectUrl = context.Request.Url.ToString();
}
return errorRedirectUrl;
}
/// <summary>
/// Authenticates external provider and make IdentityServer challenge
/// </summary>
/// <param name="externalProviderName">Provider name.</param>
/// <param name="context">Current http context from controller</param>
public void AuthenticateExternal(string externalProviderName, HttpContextBase context)
{
var widgetUrl = context.Request.Url.ToString();
var owinContext = context.Request.GetOwinContext();
var returnUrl = this.GetReturnURL(context);
var challengeProperties = ChallengeProperties.ForExternalUser(externalProviderName, widgetUrl, returnUrl);
challengeProperties.RedirectUri = returnUrl;
owinContext.Authentication.Challenge(challengeProperties, externalProviderName);
}
#endregion
#region Private Fields and methods
/// <summary>
/// Tries the resolve URL from URL referrer.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="redirectUrl">The redirect URL.</param>
/// <returns></returns>
private bool TryResolveUrlFromUrlReferrer(HttpContextBase context, out string redirectUrl)
{
redirectUrl = string.Empty;
try
{
// There are a few ways to redirect to another page
// First method is to combine realm param with redirect_uri param to get the full redirect url
// Example: ?realm=http://localhost:8086/&redirect_uri=/Sitefinity/Dashboard
// Second method is to use only realm or redirect_uri param to get the full redirect url
// Examples: ?realm=http://localhost:8086/Sitefinity/Dashboard
// ?redirect_uri=http://localhost:8086/Sitefinity/Dashboard
// Third method is to get ReturnUrl param
// Example: ?ReturnUrl=http://localhost:8086/Sitefinity/Dashboard
var urlReferrer = context.Request.UrlReferrer;
if (urlReferrer == null)
return false;
var querySegment = urlReferrer.Query.TrimStart('?');
// Check query string for all search params
string realm = string.Empty;
string redirectUri = string.Empty;
string returnUrl = string.Empty;
var queryParameters = querySegment.Split('&');
foreach (var param in queryParameters)
{
var queryParamKeyValuePair = param.Split('=');
switch (queryParamKeyValuePair[0])
{
case "realm":
realm = queryParamKeyValuePair[1].UrlDecode();
break;
case "redirect_uri":
redirectUri = queryParamKeyValuePair[1].UrlDecode();
break;
default:
if (queryParamKeyValuePair[0] == SecurityManager.AuthenticationReturnUrl)
{
returnUrl = queryParamKeyValuePair[1].UrlDecode();
}
break;
}
if (!string.IsNullOrEmpty(returnUrl))
break;
}
// Based on the params get the correct redirect url
redirectUrl = this.BuildRedirectUrl(realm, redirectUri, returnUrl);
return true;
}
catch (UriFormatException)
{
// According to the documentation (http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx),
// UrlReferrer could throw UriFormatException in case The HTTP Referer request header is malformed and cannot be converted to a Uri object.
return false;
}
}
private string BuildRedirectUrl(string realm, string redirectUri, string returnUrl)
{
string redirectUrl = null;
// Based on the found params get the correct redirectUrl
if (!string.IsNullOrWhiteSpace(realm))
{
redirectUrl = realm;
// If both realm and redirect_uri are provided, they should be combined
if (!string.IsNullOrWhiteSpace(redirectUri))
{
redirectUrl = string.Format("{0}/{1}", redirectUrl.TrimEnd('/'), redirectUri.TrimStart('/'));
}
}
else if (!string.IsNullOrWhiteSpace(redirectUri))
{
redirectUrl = redirectUri;
}
else if (!string.IsNullOrWhiteSpace(returnUrl))
{
string query = string.Empty;
Uri uri = null;
if (Uri.TryCreate(returnUrl, UriKind.Absolute, out uri))
query = uri.Query;
string pageUrlForCurrentCulture = null;
Web.Utilities.LinkParser.TryGetPageUrlForCulture(returnUrl, true, out pageUrlForCurrentCulture, null);
if (!string.IsNullOrEmpty(pageUrlForCurrentCulture))
{
var redirectUriBuilder = new UriBuilder(pageUrlForCurrentCulture);
redirectUriBuilder.Query = query.TrimStart('?');
redirectUrl = redirectUriBuilder.Uri.ToString();
}
else
{
redirectUrl = returnUrl;
}
}
return redirectUrl;
}
/// <summary>
/// Gets the claims issuer.
/// </summary>
/// <returns></returns>
private string GetClaimsIssuer()
{
var claimsModule = ClaimsManager.CurrentAuthenticationModule;
if (claimsModule != null)
{
return claimsModule.GetIssuer();
}
else
{
return LoginFormModel.DefaultRealmConfig;
}
}
/// <summary>
/// Inners the get user identifier.
/// </summary>
/// <returns>
/// The user id or null.
/// </returns>
private Guid GetUserId(NameValueCollection securityParams)
{
var cip = SecurityManager.GetManager().GetPasswordRecoveryUser(securityParams);
if (cip != null)
{
return cip.UserId;
}
return Guid.Empty;
}
/// <summary>
/// Gets ReturnURL set by administrator or taken from query string
/// </summary>
/// <returns>
/// ReturnURL to redirect or empty string
/// </returns>
protected internal string GetReturnURL(HttpContextBase context)
{
var redirectUrl = context.Request.UrlReferrer?.AbsoluteUri ?? RouteHelper.ResolveUrl(context.Request.AppRelativeCurrentExecutionFilePath, UrlResolveOptions.Absolute);
if (!string.IsNullOrEmpty(context.Request.Url.Query))
{
// Remove err flag in redirect data
redirectUrl = redirectUrl.Replace("&err=true", string.Empty).Replace("err=true", string.Empty);
}
if (this.LoginRedirectPageId.HasValue)
{
redirectUrl = this.GetPageUrl(this.LoginRedirectPageId);
}
else
{
// Get redirectUrl from query string parameter
string redirectUrlFromQS;
this.TryResolveUrlFromUrlReferrer(context, out redirectUrlFromQS);
if (!string.IsNullOrWhiteSpace(redirectUrlFromQS))
{
redirectUrl = redirectUrlFromQS;
}
}
return redirectUrl;
}
private string serviceUrl;
private const string DefaultRealmConfig = "http://localhost";
private const string LoginControllerKey = "sf_login_cntrl_id";
private string membershipProvider;
private string serializedExternalProviders;
#endregion
}
}