public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new tshiloboUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); var userId = await _userManager.GetUserIdAsync(user); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = userId, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
private async Task LoadAsync(tshiloboUser user) { var email = await _userManager.GetEmailAsync(user); Email = email; Input = new InputModel { NewEmail = email, }; IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user); }
public static async Task CreateRoleAsync(IServiceProvider serviceProvider, IConfiguration configuration) { //adding custom roles var RoleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); var UserManager = serviceProvider.GetRequiredService <UserManager <tshiloboUser> >(); string[] roleNames = { "SuperUser", "DeptAdmin", "ChurchAdmin", "ChurchMember", "AppUser", "MainLeader", "SubLeader" }; IdentityResult roleResult; foreach (var roleName in roleNames) { // creating the roles and seeding them to the database var roleExists = await RoleManager.RoleExistsAsync(roleName); if (!roleExists) { roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName)); } } //creating a super user who could maintain the web app var superuser = new tshiloboUser { Email = configuration.GetSection("UserSettings")["UserEmail"], UserName = configuration.GetSection("UserSettings")["UserName"], FirstName = configuration.GetSection("UserSettings")["UserFirstName"], LastName = configuration.GetSection("UserSettings")["UserLastName"], DisplayName = configuration.GetSection("UserSettings")["UserDisplayName"], DateOfBirth = new DateTime(1990, 1, 1, 0, 0, 0), GenderId = Convert.ToInt32(configuration.GetSection("UserSettings")["UserGenderId"]) }; string UserPassword = configuration.GetSection("UserSettings")["UserPassword"]; var user = await UserManager.FindByEmailAsync(configuration.GetSection("UserSettings")["UserEmail"]); if (user == null) { var createSuperUser = await UserManager.CreateAsync(superuser, UserPassword); if (createSuperUser.Succeeded) { // here we assign the new user the "SuperUser" role await UserManager.AddToRoleAsync(superuser, "SuperUser"); } } }
private async Task LoadSharedKeyAndQrCodeUriAsync(tshiloboUser user) { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } SharedKey = FormatKey(unformattedKey); var email = await _userManager.GetEmailAsync(user); AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new tshiloboUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public static void SeedUsers(UserManager <IdentityUser> userManager) { if (userManager.FindByEmailAsync("*****@*****.**").Result == null) { tshiloboUser user = new tshiloboUser { UserName = "******", Email = "*****@*****.**", DisplayName = "Tshilobo Admin", LastName = "Admin", FirstName = "Tshilobo", GenderId = 1, DateOfBirth = new DateTime(Convert.ToInt32(1991), Convert.ToInt32(02), Convert.ToInt32(23), 0, 0, 0) }; IdentityResult result = userManager.CreateAsync(user, "P0dC@5tHome").Result; if (result.Succeeded) { userManager.AddToRoleAsync(user, "Admin").Wait(); } } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (Gender == null || Day == null || Month == null || Year == null) { Gender = _listItem.GetGenderAsync(); } Day = _listItem.GetDayAsync(); Month = _listItem.GetMonthAsync(); Year = _listItem.GetYearAsync(); if (ModelState.IsValid) { if (DOBValidator()) { var user = new tshiloboUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, GenderId = Convert.ToInt32(Input.GenderId), DisplayName = Input.FirstName + " " + Input.LastName, DateOfBirth = new DateTime(Convert.ToInt32(Input.BirthYear), Convert.ToInt32(Input.Month), Convert.ToInt32(Input.Day), 0, 0, 0) }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { // Add user to default role await _userManager.AddToRoleAsync(user, "AppUser"); _logger.LogInformation("User Created A New Account With Password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm Your Email", $"Please Confirm Your Account By <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>Clicking Here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } else { RegistrationStatusMessage = "You provided an invalid date of birth."; return(Page()); } } // If we got this far, something failed, redisplay form return(Page()); }