protected override ValidationResult IsValid(object value, ValidationContext validationContext) { DateTime birthdayy = DateTime.Parse(value.ToString()); int age = YearCalculator.CalculateYear(birthdayy); if (age < MINYEAR) { var result = new ValidationResult(YEARSOLD); return(result); } return(null); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl ??= this.Url.Content("~/"); this.ExternalLogins = (await this.signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (!this.IsImageValid(this.Input.Images)) { this.ModelState.AddModelError("Images", "Invalid Images"); } if (this.ModelState.IsValid) { var age = YearCalculator.CalculateYear(this.Input.DateOfBirth); var user = new ApplicationUser { UserName = this.Input.Email, Email = this.Input.Email, PhoneNumber = this.Input.PhoneNumber, Age = age, }; var result = await this.userManager.CreateAsync(user, this.Input.Password); if (this.Input.Images.Any()) { await this.imageService.UploadImages(this.Input.Images.ToList(), this.Input.Images.Count(), user.Id); } if (result.Succeeded) { this.logger.LogInformation("User created a new account with password."); var code = await this.userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = this.Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, protocol: this.Request.Scheme); await this.emailSender.SendEmailAsync( this.Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (this.userManager.Options.SignIn.RequireConfirmedAccount) { return(this.RedirectToPage("RegisterConfirmation", new { email = this.Input.Email, returnUrl = returnUrl })); } else { await this.signInManager.SignInAsync(user, isPersistent : false); return(this.LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { this.ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(this.Page()); }