private async Task LoadSharedKeyAndQrCodeUriAsync(FrontEndUser 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 void SeedUsers(UserManager <FrontEndUser> userManager) { if (userManager.FindByNameAsync("*****@*****.**").Result == null) { FrontEndUser user = new FrontEndUser(); user.UserName = "******"; user.Email = "*****@*****.**"; IdentityResult result = userManager.CreateAsync (user, "Qwerty12#").Result; if (result.Succeeded) { userManager.AddToRoleAsync(user, "Administrator").Wait(); } } }
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 FrontEndUser { 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 async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new FrontEndUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _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); } } // If we got this far, something failed, redisplay form return(Page()); }
private async Task LoadAsync(FrontEndUser user) { var userName = await _userManager.GetUserNameAsync(user); //var phoneNumber = await _userManager.GetPhoneNumberAsync(user); // Gets Person in Api by Identity-User var person = await _service.GetPerson(User); Username = userName; Input = new InputModel { FirstName = person.FirstName, LastName = person.LastName, PhoneNumber = person.PhoneNumber, Address = person.Address, PostalCode = person.PostalCode }; // Setting PostalCodes await SetPostalCodes(); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new FrontEndUser { UserName = Input.Email, Email = Input.Email }; var person = new Person { Email = Input.Email, SocialSecurityNumber = Input.SocialSecurityNumber, FirstName = Input.FirstName, LastName = Input.LastName, PhoneNumber = Input.PhoneNumber, Address = Input.Address, PostalCode = Input.PostalCode }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { // Setting Userrole to Person var newUser = await _userManager.FindByNameAsync(user.UserName); var setRole = await _userManager.AddToRoleAsync(newUser, "Customer"); // Creating new Person in Api bool PostPerson = await _service.PostPerson(person); if (false == PostPerson || false == setRole.Succeeded) { // TODO: if cant create Person/or add Role, must add some Error Handling, maybe delete User in IdentityDB?? // Setting PostalCodes await SetPostalCodes(); return(Page()); } _logger.LogInformation("User created a new account with password."); 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 = user.Id, code = code, returnUrl = 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", new { email = Input.Email, returnUrl = 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()); }