public async Task <ActionResult> Create([Bind("Email,Password,ConfirmPassword,LockoutEnabled,EmailConfirmed,IsAdministrator,IsPremiumUser,IsTrialUser")] InputModel inputModel) { try { var user = new ShootingWebAgentUser { UserName = inputModel.Email, Email = inputModel.Email, LockoutEnabled = inputModel.LockoutEnabled, EmailConfirmed = inputModel.EmailConfirmed }; var result = await _userManager.CreateAsync(user, inputModel.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); } else { _logger.LogError($"Couldn't create user {user.Id}"); // return Redirect("~/Home/Error"); } IdentityResult addRoleResult; if (inputModel.IsAdministrator) { addRoleResult = await _userManager.AddToRoleAsync(user, Roles.Administrator.ToString()); if (addRoleResult.Succeeded) { _logger.LogInformation($"{Roles.Administrator} Role added to {user.UserName}."); } } if (inputModel.IsPremiumUser) { addRoleResult = await _userManager.AddToRoleAsync(user, Roles.PremiumUser.ToString()); if (addRoleResult.Succeeded) { _logger.LogInformation($"{Roles.TrialUser} Role added to {user.UserName}."); } } if (inputModel.IsTrialUser) { addRoleResult = await _userManager.AddToRoleAsync(user, Roles.TrialUser.ToString()); if (addRoleResult.Succeeded) { _logger.LogInformation($"{Roles.TrialUser} Role added to {user.UserName}."); } } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
private async Task LoadAsync(ShootingWebAgentUser user) { var userName = await _userManager.GetUserNameAsync(user); var phoneNumber = await _userManager.GetPhoneNumberAsync(user); Username = userName; Input = new InputModel { PhoneNumber = phoneNumber }; }
private EditModel GetEditModelFromUser(ShootingWebAgentUser user) { return(new EditModel() { Id = user.Id, Email = user.Email, LockoutEnabled = user.LockoutEnabled, EmailConfirmed = user.EmailConfirmed, IsAdministrator = _userManager.IsInRoleAsync(user, Roles.Administrator.ToString()).Result, IsPremiumUser = _userManager.IsInRoleAsync(user, Roles.PremiumUser.ToString()).Result, IsTrialUser = _userManager.IsInRoleAsync(user, Roles.TrialUser.ToString()).Result, }); }
private async Task LoadSharedKeyAndQrCodeUriAsync(ShootingWebAgentUser 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> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new ShootingWebAgentUser { 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."); try { await CreateRolesIfNotExist(); } catch (Exception) { return(Redirect("~/Home/Error")); } #region Add Role to User IdentityResult addRoleResult = null; if (Input.IsAdministrator) { addRoleResult = await _userManager.AddToRoleAsync(user, Roles.Administrator.ToString()); if (addRoleResult.Succeeded) { _logger.LogInformation($"{Roles.Administrator} Role added to {user.UserName}."); } } else { addRoleResult = await _userManager.AddToRoleAsync(user, Roles.TrialUser.ToString()); if (addRoleResult.Succeeded) { _logger.LogInformation($"{Roles.TrialUser} Role added to {user.UserName}."); } } #endregion 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()); }