private async Task <WebCoreUser> CreateUserAsync(string userName , string password , string firstName , string lastName , string email , string phoneNumber , string carrer , string[] roles) { WebCoreUser webCoreUser = new WebCoreUser { UserName = userName, FirstName = firstName, LastName = lastName, Email = email, PhoneNumber = phoneNumber, Carrer = carrer, EmailConfirmed = true, RecordStatus = 0 }; IdentityResult result = await userManager.CreateAsync(webCoreUser, password); foreach (string role in roles) { await userManager.AddToRoleAsync(webCoreUser, role); } if (!result.Succeeded) { throw new Exception($"Seeding \"{userName}\" user failed. Errors: {string.Join(Environment.NewLine, result.Errors.ToArray().Select(x => x.Description))}"); } return(webCoreUser); }
public async Task <string[]> GetAllClaimsAsync(EntityId <string> userId) { WebCoreUser user = await userManager.FindByIdAsync(userId.Id); IList <Claim> claims = await userManager.GetClaimsAsync(user); return(claims.Select(x => x.Value).ToArray()); }
public async Task <bool> SetActiveAsync(EntityId <string> entityId, long recordStatus) { WebCoreUser entity = userRepository.GetById(entityId.Id); entity.RecordStatus = recordStatus; entity.ModifiedBy = GetCurrentUserLogin(); entity.ModifiedDate = DateTime.Now; entity.UpdateToken = Guid.NewGuid(); IdentityResult result = await userManager.UpdateAsync(entity); return(result.Succeeded); }
public async Task <bool> Delete(EntityId <string> entityId) { WebCoreUser entity = userRepository.GetById(entityId.Id); entity.RecordStatus = ConstantConfig.UserRecordStatus.Deleted; entity.ModifiedBy = GetCurrentUserLogin(); entity.ModifiedDate = DateTime.Now; entity.UpdateToken = Guid.NewGuid(); IdentityResult result = await userManager.UpdateAsync(entity); return(result.Succeeded); }
public async Task <bool> Add(UserInfoInput addInput) { WebCoreUser entity = mapper.Map <WebCoreUser>(addInput); entity.CreatedBy = GetCurrentUserLogin(); entity.CreatedDate = DateTime.Now; entity.ModifiedBy = entity.CreatedBy; entity.ModifiedDate = entity.CreatedDate; entity.UpdateToken = Guid.NewGuid(); IdentityResult result = await userManager.CreateAsync(entity); return(result.Succeeded); }
public UserResetPasswordInput GetResetPasswordInputById(EntityId <string> entityId) { WebCoreUser entity = userRepository.GetById(entityId.Id); UserResetPasswordInput updateInput = new UserResetPasswordInput(); if (entity == null) { return(null); } updateInput = mapper.Map <UserResetPasswordInput>(entity); return(updateInput); }
public async Task <IActionResult> InputInfoPartial(UserInfoInput inputModel) { if (inputModel.Id != null) { //update WebCoreUser lastInfo = userService.GetById(inputModel); if (lastInfo.UpdateToken.GetValueOrDefault(Guid.Empty).Equals(inputModel.UpdateToken)) { await userService.UpdateInfo(inputModel); return(Ok(new { result = ConstantConfig.WebApiStatusCode.Success, message = GetLang(ConstantConfig.WebApiResultMessage.UpdateSuccess) })); } return(Ok(new { result = ConstantConfig.WebApiStatusCode.Warning, message = GetLang(ConstantConfig.WebApiResultMessage.UpdateTokenNotMatch) })); } return(Ok(new { result = ConstantConfig.WebApiStatusCode.Error, message = GetLang(ConstantConfig.WebApiResultMessage.Error) })); }
private async Task LoadSharedKeyAndQrCodeUriAsync(WebCoreUser 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> AssignPermissionPartial(EntityId <string> idModel = null) { WebCoreUser user = userService.GetById(idModel); UserAssignPermissionViewModel viewModel = new UserAssignPermissionViewModel(); if (user == null) { return(Forbid()); } string[] allClaims = await userService.GetAllClaimsAsync(idModel); viewModel.TreeViewPermission = await permissionService.GetPermissionTreeViewAsync(allClaims); viewModel.AllRoles = await userService.GetAllRolesAsync(idModel); ViewBag.UserId = idModel.Id; return(PartialView(viewModel)); }
public async Task <bool> UpdateInfo(UserInfoInput updateInput) { WebCoreUser entity = userRepository.GetById(updateInput.Id); if (entity == null) { return(false); } mapper.Map(updateInput, entity); entity.RecordStatus = ConstantConfig.UserRecordStatus.Active; entity.ModifiedBy = GetCurrentUserLogin(); entity.ModifiedDate = DateTime.Now; entity.UpdateToken = Guid.NewGuid(); IdentityResult result = await userManager.UpdateAsync(entity); return(result.Succeeded); }
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 WebCoreUser { 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 <List <RoleDto> > GetAllRolesAsync(EntityId <string> userId) { List <RoleDto> roles = roleRepository.GetAll().Select(x => new RoleDto() { RoleName = x.Name, IsChecked = false }).ToList(); WebCoreUser user = userRepository.GetById(userId.Id); IList <string> allRolesOfUser = await userManager.GetRolesAsync(user); foreach (RoleDto role in roles) { if (allRolesOfUser.Any(x => x == role.RoleName)) { role.IsChecked = true; } } return(roles); }
public async Task <IActionResult> AssignPermissionPartial(AssignPermissionInput assignPermissionInput) { WebCoreUser user = userService.GetById(new EntityId <string>() { Id = assignPermissionInput.UserId }); if (user == null) { return(Ok(new { result = ConstantConfig.WebApiStatusCode.Error, message = GetLang(ConstantConfig.WebApiResultMessage.Error) })); } bool success = await userService.UpdatePermissionsAsync(assignPermissionInput); if (success) { return(Ok(new { result = ConstantConfig.WebApiStatusCode.Success, message = GetLang(ConstantConfig.WebApiResultMessage.UpdateSuccess) })); } else { return(Ok(new { result = ConstantConfig.WebApiStatusCode.Error, message = GetLang(ConstantConfig.WebApiResultMessage.Error) })); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new WebCoreUser { 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()); }
public async Task <bool> UpdatePermissionsAsync(AssignPermissionInput assignPermissionInput) { WebCoreUser user = userRepository.GetById(assignPermissionInput.UserId); string[] viewRoles = assignPermissionInput.Roles; EntityId <string> userIdModel = new EntityId <string>() { Id = assignPermissionInput.UserId }; List <RoleDto> allRoles = await GetAllRolesAsync(userIdModel); string[] allUserRoles = allRoles.Where(x => x.IsChecked).Select(x => x.RoleName).ToArray(); HashSet <string> allClaims = permissionService.GetAllPermissions(); IList <Claim> allClaimsOfUser = await userManager.GetClaimsAsync(user); string[] viewClaims = assignPermissionInput.Permissions; if (viewRoles.Any(vr => allRoles.Count(r => r.RoleName == vr) == 0)) { return(false); } if (viewClaims.Any(vc => allClaims.Count(ac => ac == vc) == 0)) { return(false); } // roles need delete List <string> rolesNeedDelete = allUserRoles .Where(ur => viewRoles.Count(vr => vr == ur) == 0) .ToList(); // roles need add List <string> rolesNeedAdd = viewRoles .Where(vr => allUserRoles.Count(ur => ur == vr) == 0) .ToList(); foreach (string roleName in rolesNeedDelete) { await userManager.RemoveFromRoleAsync(user, roleName); } foreach (string roleName in rolesNeedAdd) { await userManager.AddToRoleAsync(user, roleName); } // claims need delete IList <Claim> claimsNeedDelete = allClaimsOfUser .Where(uc => viewClaims.Count(vc => vc == uc.Value) == 0) .ToList(); // claims need add List <string> claimsNeedAdd = viewClaims .Where(vc => allClaimsOfUser.Count(uc => vc == uc.Value) == 0) .ToList(); foreach (string claim in claimsNeedAdd) { await userManager.AddClaimAsync(user, new Claim(ConstantConfig.ClaimType.Permission, claim)); } foreach (var claim in claimsNeedDelete) { await userManager.RemoveClaimAsync(user, claim); } return(true); }