public DiscountsUser CreateUser(DiscountsUser user, IList <string> roles) { using (var tran = _context.Database.BeginTransaction()) { var roleObjs = _context.Roles.Where(x => roles.Contains(x.Name)).ToList(); var newUser = _context.Users.Add(user).Entity; _context.SaveChanges(); var toAdd = roleObjs.Select(x => new DiscountsUserRole() { UserId = user.Id, RoleId = x.Id }); if (toAdd.Count() > 0) { _context.UserRoles.AddRange(toAdd); } _context.SaveChanges(); tran.Commit(); return(newUser); } }
public void UpdateUser(DiscountsUser user, IList <string> roles) { using (var tran = _context.Database.BeginTransaction()) { var roleObjs = _context.Roles.Where(x => roles.Contains(x.Name)).ToList(); var currentRoleMaps = _context.Users.Update(user).Entity.UserRoleMaps; var toRemove = currentRoleMaps.Where(x => !roles.Contains(x.Role.Name)); if (toRemove.Count() > 0) { _context.UserRoles.RemoveRange(toRemove); } var toAdd = roleObjs.Where(x => !currentRoleMaps.Select(y => y.RoleId).Contains(x.Id)).Select(x => new DiscountsUserRole() { UserId = user.Id, RoleId = x.Id }); if (toAdd.Count() > 0) { _context.UserRoles.AddRange(toAdd); } _context.SaveChanges(); tran.Commit(); } }
public void CreateUser(UserModel user) { var dUser = new DiscountsUser(); dUser.ConcurrencyStamp = user.ConcurrencyStamp; dUser.LockoutEnabled = user.LockoutEnabled; dUser.LockoutEnd = user.LockoutEnd; dUser.PartnerId = user.PartnerId; dUser.UserName = user.UserName; dUser.NormalizedUserName = user.UserName.ToUpper(); dUser.Email = user.Email; dUser.NormalizedEmail = user.Email.ToUpper(); dUser.PhoneNumber = user.PhoneNumber; _userService.CreateUser(dUser, user.Roles); }
private async Task LoadSharedKeyAndQrCodeUriAsync(DiscountsUser 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 DiscountsUser { 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 DiscountsUser { 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()); }