using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using RazorAPP.Data; using System.ComponentModel.DataAnnotations; namespace RazorAPP.Pages { public class RegisterModel : PageModel { private readonly SignInManager _signInManager; private readonly UserManager _userManager; private readonly ILogger _logger; ////comented the Iemailsender because its causing error. // private readonly IEmailSender _emailSender; //// added by me for dependency injection; private readonly RoleManager _roleManager; private readonly ApplicationDbContext _db; public RegisterModel( UserManager userManager, SignInManager signInManager, ILogger logger, // IEmailSender emailSender, ////added by me for constructor for the upper used dependency injection; RoleManager roleManager, ApplicationDbContext db) { _userManager = userManager; _signInManager = signInManager; _logger = logger; // _emailSender = emailSender; ////added by me for upper used constructor; _roleManager = roleManager; _db = db; } [BindProperty] public InputModel Input { get; set; } public string ReturnUrl { get; set; } public IList ExternalLogins { get; set; } public class InputModel { [Required] [EmailAddress] [Display(Name = "Email")] public string Email { 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; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } //added by me //[BindProperty] //public string Gender { get; set; } //public string[] Genders = new[] { "Male", "Female", "Unspecified" }; //[DataType(DataType.Date)] //[Column(TypeName = "Date")] //public DateTime DateOfBirth { get; set; } [Required] [RegularExpression("([a-zA-Z][a-zA-Z ]+)", ErrorMessage = "Only alphabets are allowed")] //public string FullName { get; set; } public string FirstName { get; set; } [RegularExpression("([a-zA-Z][a-zA-Z ]+)", ErrorMessage = "Only alphabets are allowed")] public string LastName { get; set; } [Required] [RegularExpression("(^.*$)", ErrorMessage = "Invalid EmployeCode")] public string EmployeCode { get; set; } public string Designation { get; set; } //[DataType(DataType.Date)] //[Column(TypeName = "Date")] [Required] [BindProperty] public DateTime DateOfBirth { get; set; } [Required] [BindProperty] public DateTime DateOfJoining { get; set; } [Required] [Display(Name = "Emergency No")] [MaxLength(10), MinLength(10)] public string EmergencyNo { get; set; } [Required] [MaxLength(12), MinLength(12)] public string AdharNo { get; set; } [Required] public string Address { get; set; } public string City { get; set; } public string PostalCode { get; set; } [Required] [Display(Name = "Phone Number")] [MaxLength(10)/*, MinLength(10)*/] public string PhoneNumber { get; set; } [Required] [BindProperty] public string Gender { get; set; } public string[] Genders = new[] { "Male", "Female", }; } public class MobileUniqueAttribute : ValidationAttribute { protected override ValidationResult IsValid( object value, ValidationContext validationContext) { var _context = (ApplicationDbContext)validationContext.GetService(typeof(ApplicationDbContext)); var entity = _context.ApplicationUser.SingleOrDefault(e => e.PhoneNumber == value.ToString()); if (entity != null) { return new ValidationResult(/*GetErrorMessage(value.ToString())*/"Hey The MobileNo is Alrdy Present"); } return ValidationResult.Success; } public string GetErrorMessage(string mobile) { return $"Mobile {mobile} is already in use."; } //ValidationResult validphone = IsValid(object value, ValidationContext validationContext); } public async Task OnGetAsync(string returnUrl = null) { ReturnUrl = returnUrl; Input = new InputModel(); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); } public async Task OnPostAsync(string returnUrl = null) { var submitdata = Input; //var _context = (ApplicationDbContext)_db.ApplicationUser(typeof(ApplicationDbContext)); returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); // If we got this far, something failed, redisplay form return Page(); } } }