From bc3001cd8b802b7f666a2c98230fd1a31240b670 Mon Sep 17 00:00:00 2001 From: aevangel Date: Mon, 13 Sep 2021 09:54:53 +0300 Subject: [PATCH] update validation signin/signup validation logic --- .../Areas/Identity/Pages/Account/Login.cshtml | 10 ++++++---- .../Identity/Pages/Account/Login.cshtml.cs | 5 +++++ .../Identity/Pages/Account/Register.cshtml | 9 +++++---- .../Identity/Pages/Account/Register.cshtml.cs | 19 ++++++++++++++++--- AdminDashboard/AdminDashboard/Startup.cs | 5 ++++- 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Login.cshtml b/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Login.cshtml index 0879194..b0c04b2 100644 --- a/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Login.cshtml +++ b/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Login.cshtml @@ -14,6 +14,11 @@ @(Html.Kendo().Form() .Name("Login") + .Validatable(v => + { + v.ValidationSummary(false); + v.ValidateOnBlur(true); + }) .FormData(Model.Input) .Layout("grid") .Grid(g => g.Cols(1).Gutter(5)) @@ -26,7 +31,7 @@ items.Add() .Field(f => f.Password) .Label(l => l.Text("Password:")) - .EditorTemplateHandler("setPasswordEditor"); + .InputHtmlAttributes(new { @type = "password", autocomplete = "off" }); items.Add() .Field(f => f.RememberMe) @@ -91,9 +96,6 @@ } diff --git a/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Login.cshtml.cs b/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Login.cshtml.cs index e282258..9b6e990 100644 --- a/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Login.cshtml.cs +++ b/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Login.cshtml.cs @@ -107,6 +107,11 @@ public async Task OnPostAsync(string returnUrl = null) _logger.LogWarning("User account locked out."); return RedirectToPage("./Lockout"); } + if (!result.Succeeded) + { + ModelState.AddModelError("Password", "Invalid username or password"); + return Page(); + } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); diff --git a/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Register.cshtml b/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Register.cshtml index 0159cfe..7b49e45 100644 --- a/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Register.cshtml +++ b/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Register.cshtml @@ -17,6 +17,10 @@ @(Html.Kendo().Form() .Name("Register") + .Validatable(v=> { + v.ValidationSummary(false); + v.ValidateOnBlur(true); + }) .FormData(Model.Input) .Layout("grid") .Grid(g => g.Cols(1).Gutter(5)) @@ -31,7 +35,7 @@ items.Add() .Field(f => f.Password) .Label(l => l.Text("Password")) - .EditorTemplateHandler("setPasswordEditor"); + .InputHtmlAttributes(new { @type = "password", autocomplete = "off" }); items.Add() .Field(f => f.Company) .Label(l => l.Text("Company")); @@ -83,9 +87,6 @@ } diff --git a/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Register.cshtml.cs b/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Register.cshtml.cs index 7c6cc4a..0dc3c1b 100644 --- a/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Register.cshtml.cs +++ b/AdminDashboard/AdminDashboard/Areas/Identity/Pages/Account/Register.cshtml.cs @@ -60,9 +60,6 @@ public class InputModel public string Company { get; set; } [Required] - [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] - [DataType(DataType.Password)] - [Display(Name = "Password")] public string Password { get; set; } public bool AgreeToTerms { get; set; } @@ -80,8 +77,23 @@ public async Task OnGetAsync(string returnUrl = null) public async Task OnPostAsync(string returnUrl = null) { + var model = Request.Form; returnUrl ??= Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); + + var userExists = await _userManager.FindByEmailAsync(Input.Email); + if (userExists != null) + { + ModelState.AddModelError("Email", "A User with this email is already registered"); + } + + var passwordValidator = new PasswordValidator(); + var PasswordValidationResult = await passwordValidator.ValidateAsync(_userManager, null, Input.Password); + if (!PasswordValidationResult.Succeeded) + { + ModelState.AddModelError("Password", "Password must contain an uppercase character, lowercase character, a digit, and a non-alphanumeric character and be at least six characters long."); + } + if (ModelState.IsValid) { var user = new ApplicationUser @@ -92,6 +104,7 @@ public async Task OnPostAsync(string returnUrl = null) FullName = Input.FullName, AgreeToTerms = Input.AgreeToTerms }; + var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { diff --git a/AdminDashboard/AdminDashboard/Startup.cs b/AdminDashboard/AdminDashboard/Startup.cs index 4894f9f..a5001ec 100644 --- a/AdminDashboard/AdminDashboard/Startup.cs +++ b/AdminDashboard/AdminDashboard/Startup.cs @@ -32,7 +32,10 @@ public void ConfigureServices(IServiceCollection services) options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); services.AddDatabaseDeveloperPageExceptionFilter(); - services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) + services.AddDefaultIdentity(options => { + options.SignIn.RequireConfirmedAccount = false; + options.User.RequireUniqueEmail = true; + }) .AddEntityFrameworkStores(); services.ConfigureApplicationCookie(options => {