public async Task <IActionResult> OnPostAsync() { string uRole = Request.Form["Roles"].ToString(); string user = Request.Form["UserID"].ToString(); string curRole = Request.Form["curRole"].ToString(); GadgetCMSUser gadgetCmsUser = await userManager.FindByIdAsync(user); var removeResult = await userManager.RemoveFromRoleAsync(gadgetCmsUser, curRole); var addResult = await userManager.AddToRoleAsync(gadgetCmsUser, uRole); if (removeResult.Succeeded && addResult.Succeeded) { return(RedirectToPage("./ManageUsers")); } else if (removeResult.Succeeded && !addResult.Succeeded) { await userManager.AddToRoleAsync(gadgetCmsUser, curRole); return(RedirectToPage("/Error")); } else { return(RedirectToPage("/Error")); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/Identity/Account/CheckEmail"); var recaptcha = await _recaptcha.Validate(Request); if (!recaptcha.success) { ModelState.AddModelError("Recaptcha", "There was an error validating recaptcha. Please try again!"); } if (ModelState.IsValid) { var user = new GadgetCMSUser { UserName = Input.Email, Email = Input.Email, Nickname = Input.Nickname }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); logger.Info("{user} created a new account", Input.Email); // Add a user to the default role, or any role you prefer here await _userManager.AddToRoleAsync(user, "Member"); 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); ViewData["NewRegister"] = true; return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
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 GadgetCMSUser { UserName = Input.Email, Email = Input.Email, EmailConfirmed = true }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { // Add a user to the default role, or any role you prefer here await _userManager.AddToRoleAsync(user, "Member"); 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); logger.Info("{user} created an account using {Name} provider.", Input.Email, info.LoginProvider); 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 LoadSharedKeyAndQrCodeUriAsync(GadgetCMSUser 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 static async Task CreateRoles(IServiceProvider serviceProvider, IConfiguration Configuration) { //adding customs roles var RoleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); var UserManager = serviceProvider.GetRequiredService <UserManager <GadgetCMSUser> >(); string[] roleNames = { "Admin", "Moderator", "Editor", "Member" }; IdentityResult roleResult; foreach (var roleName in roleNames) { // creating the roles and seeding them to the database var roleExist = await RoleManager.RoleExistsAsync(roleName); if (!roleExist) { roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName)); } } // creating a super user who could maintain the web app var poweruser = new GadgetCMSUser { UserName = Configuration.GetSection("AppSettings")["UserEmail"], Email = Configuration.GetSection("AppSettings")["UserEmail"] }; string userPassword = Configuration.GetSection("AppSettings")["UserPassword"]; var user = await UserManager.FindByEmailAsync(Configuration.GetSection("AppSettings")["UserEmail"]); if (user == null) { var createPowerUser = await UserManager.CreateAsync(poweruser, userPassword); if (createPowerUser.Succeeded) { // here we assign the new user the "Admin" role await UserManager.AddToRoleAsync(poweruser, "Admin"); } } }