Skip to content

Commit

Permalink
update validation signin/signup validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandarevangelatov committed Sep 13, 2021
1 parent e6280f5 commit bc3001c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
</div>
@(Html.Kendo().Form<LoginModel.InputModel>()
.Name("Login")
.Validatable(v =>
{
v.ValidationSummary(false);
v.ValidateOnBlur(true);
})
.FormData(Model.Input)
.Layout("grid")
.Grid(g => g.Cols(1).Gutter(5))
Expand All @@ -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)
Expand Down Expand Up @@ -91,9 +96,6 @@
}
</style>
<script>
function setPasswordEditor(container, options) {
container.append($("<input type='password' class='k-textbox k-valid' id='Password' name='Password' title='Password' required='required' autocomplete='off' aria-labelledby='Password-form-label' data-bind='value:Password' aria-describedby='Password-form-hint'>"));
}
$("#Login").append($("<input type='hidden' name='__RequestVerificationToken' value='@token' data-stop='true' />"))
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public async Task<IActionResult> 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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
</div>
@(Html.Kendo().Form<RegisterModel.InputModel>()
.Name("Register")
.Validatable(v=> {
v.ValidationSummary(false);
v.ValidateOnBlur(true);
})
.FormData(Model.Input)
.Layout("grid")
.Grid(g => g.Cols(1).Gutter(5))
Expand All @@ -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"));
Expand Down Expand Up @@ -83,9 +87,6 @@
}
</style>
<script>
function setPasswordEditor(container, options) {
container.append($("<input type='password' class='k-textbox k-valid' id='Password' name='Password' title='Password' required='required' autocomplete='off' aria-labelledby='Password-form-label' data-bind='value:Password' aria-describedby='Password-form-hint'>"));
}
$("#Register").append($("<input type='hidden' name='__RequestVerificationToken' value='@token' data-stop='true' />"))
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -80,8 +77,23 @@ public async Task OnGetAsync(string returnUrl = null)

public async Task<IActionResult> 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<ApplicationUser>();
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
Expand All @@ -92,6 +104,7 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
FullName = Input.FullName,
AgreeToTerms = Input.AgreeToTerms
};

var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
Expand Down
5 changes: 4 additions & 1 deletion AdminDashboard/AdminDashboard/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public void ConfigureServices(IServiceCollection services)
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
services.AddDefaultIdentity<ApplicationUser>(options => {
options.SignIn.RequireConfirmedAccount = false;
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>();
services.ConfigureApplicationCookie(options =>
{
Expand Down

0 comments on commit bc3001c

Please sign in to comment.