public async Task <IActionResult> OnPostAccountDataChangeAsync(BingolUser model) { if (!ModelState.IsValid) { return(RedirectToAction("Account")); } var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } user.UserFirstName = model.UserFirstName; user.UserLastName = model.UserLastName; user.PhoneNumber = model.PhoneNumber; user.Email = model.Email; user.UserName = model.Email; user.EmailConfirmed = true; var result = await _userManager.UpdateAsync(user); if (!result.Succeeded) { ViewBag.StatusMessage = "Unexpected error when trying to set details."; return(RedirectToAction("Account")); } await _signInManager.RefreshSignInAsync(user); ViewBag.StatusMessage = "Your profile has been updated"; return(RedirectToAction("Account")); }
private static void CreateRoles(IServiceProvider serviceProvider) { var roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); var userManager = serviceProvider.GetRequiredService <UserManager <BingolUser> >(); Task <IdentityResult> roleResult; const string username = "******"; //Check that there is an Administrator role and create if not var hasAdminRole = roleManager.RoleExistsAsync("Admin"); hasAdminRole.Wait(); var hasCustomerRole = roleManager.RoleExistsAsync("Customer"); hasCustomerRole.Wait(); if (!hasAdminRole.Result) { roleResult = roleManager.CreateAsync(new IdentityRole("Admin")); roleResult.Wait(); } if (!hasCustomerRole.Result) { roleResult = roleManager.CreateAsync(new IdentityRole("Customer")); roleResult.Wait(); } //Check if the admin user exists and create it if not //Add to the Administrator role var testUser = userManager.FindByNameAsync(username); testUser.Wait(); if (testUser.Result != null) { return; } var administrator = new BingolUser { UserFirstName = "Bingol", UserLastName = "Bingol", PhoneNumber = "+880123493249", EmailConfirmed = true, Email = username, UserName = username }; var newUser = userManager.CreateAsync(administrator, "Bingol123@"); newUser.Wait(); if (!newUser.Result.Succeeded) { return; } var newUserRole = userManager.AddToRoleAsync(administrator, "Admin"); newUserRole.Wait(); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl ??= Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new BingolUser { UserFirstName = Input.FirstName, UserLastName = Input.LastName, UserName = Input.Email, Email = Input.Email, PhoneNumber = Input.PhoneNumber, UserCity = Input.City, UserState = Input.State, UserZip = Input.ZipCode, UserCountry = Input.Country, UserAddress = Input.BillingAddress, UserAddress2 = Input.ShippingAddress, UserRegistrationDate = DateTime.Now, UserIp = new WebClient().DownloadString(IchanZip).TrimEnd(), EmailConfirmed = true, }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); await _userManager.AddToRoleAsync(user, "Customer"); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: (area: "Identity", userId: user.Id, code, returnUrl), 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>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", (email: Input.Email, returnUrl))); } else { await _signInManager.SignInAsync(user, isPersistent : false); 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()); }
private async Task LoadAsync(BingolUser user) { var email = await _userManager.GetEmailAsync(user); Email = email; Input = new InputModel { NewEmail = email, }; IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user); }
private async Task LoadAsync(BingolUser user) { var userName = await _userManager.GetUserNameAsync(user); UserName = userName; var firstName = user.UserFirstName; var lastName = user.UserLastName; var phoneNumber = user.PhoneNumber; var email = user.Email; Input = new InputModel { FirstName = firstName, LastName = lastName, PhoneNumber = phoneNumber, Email = email }; }
private async Task LoadSharedKeyAndQrCodeUriAsync(BingolUser 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); }
private async Task LoadAsync(BingolUser user) { var userName = await _userManager.GetUserNameAsync(user); Username = userName; var city = user.UserCity; var state = user.UserState; var zip = user.UserZip; var country = user.UserCountry; var billingAddress = user.UserAddress; var shippingAddress = user.UserAddress2; Input = new InputModel { City = city, State = state, ZipCode = zip, Country = country, BillingAddress = billingAddress, ShippingAddress = shippingAddress }; }
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 BingolUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { _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>."); // If account confirmation is required, we need to show the link if we don't have a real email sender if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("./RegisterConfirmation", new { Email = Input.Email })); } await _signInManager.SignInAsync(user, isPersistent : false, info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } ProviderDisplayName = info.ProviderDisplayName; ReturnUrl = returnUrl; return(Page()); }