/// <summary> /// Stores any authentication tokens found in the external authentication cookie into the associated user. /// </summary> /// <param name="externalLogin">The information from the external login provider.</param> /// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/> of the operation.</returns> public virtual async Task <IdentityResult> UpdateExternalAuthenticationTokensAsync(ExternalLoginInfo externalLogin) { externalLogin.CheakArgument(); if (externalLogin.AuthenticationTokens != null && externalLogin.AuthenticationTokens.Any()) { var user = await UserManager.FindByLoginAsync(externalLogin.LoginProvider, externalLogin.ProviderKey); if (user == null) { return(IdentityResult.Failed()); } foreach (var token in externalLogin.AuthenticationTokens) { var result = await UserManager.SetAuthenticationTokenAsync(user, externalLogin.LoginProvider, token.Name, token.Value); if (!result.Succeeded) { return(result); } } } return(IdentityResult.Success()); }
public async Task <IdentityResult <TokenCredentials> > LoginUserAsync(LoginViewModel model) { if (await _userManager.FindByEmailAsync(model.Email).ConfigureAwait(false) is var user && user == null) { return(IdentityResult <TokenCredentials> .Failed(IdentityErrors.UserNotFound)); } if (!await _userManager.IsEmailConfirmedAsync(user).ConfigureAwait(false)) { return(IdentityResult <TokenCredentials> .Failed(IdentityErrors.EmailNotConfirmed)); } var result = await _signInManager.PasswordSignInAsync( user, model.Password, model.RememberMe, false).ConfigureAwait(false); if (!result.Succeeded) { return(IdentityResult <TokenCredentials> .Failed(result.TryGetIdentityError())); } var token = await _tokenService.GetTokenAsync(user).ConfigureAwait(false); return(IdentityResult <TokenCredentials> .Success(token)); }
private void Reject_Click(object sender, RoutedEventArgs e) { Button senderButton = sender as Button; IdentityResult result; AppUser user; if (senderButton.DataContext is AppUser) { user = (AppUser)senderButton.DataContext; result = helper.RejectRegisterRequest(user); if (result.AreSameWith(IdentityResult.Success())) { MessageBox.Show("拒绝用户审批成功!"); approvallist.Remove(user); approval_list.ItemsSource = null; approval_list.ItemsSource = approvallist; } else { MessageBox.Show(string.Join(",", result.Errors)); } } else { result = IdentityResult.Error("待审批的用户不存在."); MessageBox.Show("错误:待审批的用户不存在."); } }
public async Task EditUserAsync() { var user = UserManager.Users.FirstOrDefault(d => d.UserName != ""); var result = await UserManager.UpdateAsync(user); Assert.IsTrue(IdentityResult.Success().Succeeded); }
public async Task <IdentityResult> ValidateUserName(IUserManager manager, User user, ICollection <IdentityError> errors) { var userName = user.UserName; if (string.IsNullOrWhiteSpace(userName)) { errors.Add(Describer.DuplicateUserName(userName)); return(IdentityResult.Failed(errors.ToArray())); } else if (!string.IsNullOrEmpty(manager.Options.User.AllowedUserNameCharacters) && userName.Any(c => !manager.Options.User.AllowedUserNameCharacters.Contains(c))) { errors.Add(Describer.InvalidUserName(userName)); return(IdentityResult.Failed(errors.ToArray())); } else { if (await manager.Users.AnyAsync(d => d.UserName.Equals(userName))) { errors.Add(new IdentityError { Code = userName, Description = $"用户名称:{userName}已经存在" }); return(IdentityResult.Failed(errors.ToArray())); } } return(IdentityResult.Success()); }
public async Task <IdentityResult> ValidatePhoneNumberAsync(IUserManager manager, User user, ICollection <IdentityError> errors) { var phoneNumber = user.PhoneNumber; if (string.IsNullOrWhiteSpace(phoneNumber)) { errors.Add(new IdentityError { Code = phoneNumber, Description = $"电话号码不能为空" }); return(IdentityResult.Failed(errors.ToArray())); } if (!new PhoneAttribute().IsValid(phoneNumber)) { errors.Add(new IdentityError { Code = phoneNumber, Description = $"电话号码:{phoneNumber}格式不正确" }); return(IdentityResult.Failed(errors.ToArray())); } if (await manager.Users.AnyAsync(d => d.PhoneNumber.Equals(phoneNumber))) { errors.Add(new IdentityError { Code = phoneNumber, Description = $"电话号码:{phoneNumber}已经存在" }); return(IdentityResult.Failed(errors.ToArray())); } return(IdentityResult.Success()); }
/// <summary> /// Sets the name of the specified <paramref name="role"/>. /// </summary> /// <param name="role">The role whose name should be set.</param> /// <param name="name">The name to set.</param> /// <returns> /// The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/> /// of the operation. /// </returns> public virtual async Task <IdentityResult> SetRoleNameAsync(TRole role, string name) { ThrowIfDisposed(); await Store.SetRoleNameAsync(role, name, CancellationToken); await UpdateNormalizedRoleNameAsync(role); return(IdentityResult.Success()); }
/// <summary> /// Validates a role as an asynchronous operation. /// </summary> /// <param name="manager">The <see cref="RoleManager"/> managing the role store.</param> /// <param name="role">The role to validate.</param> /// <returns>A <see cref="Task{TResult}"/> that represents the <see cref="IdentityResult"/> of the asynchronous validation.</returns> public virtual async Task <IdentityResult> ValidateAsync(IRoleManager manager, Role role) { manager.CheakArgument(); role.CheakArgument(); var errors = new List <IdentityError>(); await ValidateRoleName(manager, role, errors); if (errors.Count > 0) { return(IdentityResult.Failed(errors.ToArray())); } return(IdentityResult.Success()); }
public virtual async Task <IdentityResult> ValidateAsync(IUserManager manager, User user) { manager.CheakArgument(); user.CheakArgument(); var errors = new List <IdentityError>(); await ValidateUserName(manager, user, errors); await ValidatePhoneNumberAsync(manager, user, errors); if (manager.Options.User.RequireUniqueEmail) { await ValidateEmail(manager, user, errors); } return(errors.Count > 0 ? IdentityResult.Failed(errors.ToArray()) : IdentityResult.Success()); }
public virtual async Task <IdentityResult> CheckPasswordAsync(string userName, string password) { var errors = new List <IdentityError>(); var user = new User(password) { UserName = userName }; var existsUser = await Repository.ExistsUserAsync(user); if (!existsUser) { errors.Add(ErrorDescriber.UserNotFound()); return(IdentityResult.Fail(errors)); } return(IdentityResult.Success()); }
/// <summary> /// Validates the specified <paramref name="user"/> as an asynchronous operation. /// </summary> /// <param name="manager">The <see cref="UserManager{TUser}"/> that can be used to retrieve user properties.</param> /// <param name="user">The user to validate.</param> /// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/> of the validation operation.</returns> public virtual async Task <IdentityResult> ValidateAsync(UserManager <TUser> manager, TUser user) { if (manager == null) { throw new ArgumentNullEx(nameof(manager)); } if (user == null) { throw new ArgumentNullEx(nameof(user)); } var errors = new List <IdentityError>(); await ValidateUserNameAsync(manager, user, errors); await ValidateEmailAsync(manager, user, errors); await ValidatePhoneAsync(manager, user, errors); return(errors.Count > 0 ? IdentityResult.Failed(errors.ToArray()) : IdentityResult.Success()); }
/// <summary> /// Validates a password as an asynchronous operation. /// </summary> /// <param name="manager">The <see cref="UserManager{TUser}"/> to retrieve the <paramref name="user"/> properties from.</param> /// <param name="user">The user whose password should be validated.</param> /// <param name="password">The password supplied for validation</param> /// <returns>The task object representing the asynchronous operation.</returns> public virtual Task <IdentityResult> ValidateAsync(UserManager <TUser> manager, TUser user, string password) { if (password == null) { throw new ArgumentNullEx(nameof(password)); } if (manager == null) { throw new ArgumentNullEx(nameof(manager)); } var errors = new List <IdentityError>(); var options = manager.Options.Password; if (string.IsNullOrWhiteSpace(password) || password.Length < options.RequiredLength) { errors.Add(Describer.PasswordTooShort(options.RequiredLength)); } if (options.RequireNonAlphanumeric && password.All(IsLetterOrDigit)) { errors.Add(Describer.PasswordRequiresNonAlphanumeric()); } if (options.RequireDigit && !password.Any(IsDigit)) { errors.Add(Describer.PasswordRequiresDigit()); } if (options.RequireLowercase && !password.Any(IsLower)) { errors.Add(Describer.PasswordRequiresLower()); } if (options.RequireUppercase && !password.Any(IsUpper)) { errors.Add(Describer.PasswordRequiresUpper()); } if (options.RequiredUniqueChars >= 1 && password.Distinct().Count() < options.RequiredUniqueChars) { errors.Add(Describer.PasswordRequiresUniqueChars(options.RequiredUniqueChars)); } return (Task.FromResult(errors.Count == 0 ? IdentityResult.Success() : IdentityResult.Failed(errors.ToArray()))); }
/// <summary> /// Should return <see cref="IdentityResult.Success()"/> if validation is successful. This is /// called before saving the role via Create or Update. /// </summary> /// <param name="role">The role</param> /// <returns>A <see cref="IdentityResult"/> representing whether validation was successful.</returns> protected virtual async Task <IdentityResult> ValidateRoleAsync(TRole role) { var errors = new List <IdentityError>(); foreach (var v in RoleValidators) { var result = await v.ValidateAsync(this, role); if (!result.Succeeded) { errors.AddRange(result.Errors); } } if (errors.Count > 0) { Logger.LogWarning(0, "Role {roleId} validation failed: {errors}.", await GetRoleIdAsync(role), string.Join(";", errors.Select(e => e.Code))); return(IdentityResult.Failed(errors.ToArray())); } return(IdentityResult.Success()); }
/// <summary> /// Validates a role as an asynchronous operation. /// </summary> /// <param name="manager">The <see cref="RoleManager{TRole}"/> managing the role store.</param> /// <param name="role">The role to validate.</param> /// <returns>A <see cref="Task{TResult}"/> that represents the <see cref="IdentityResult"/> of the asynchronous validation.</returns> public virtual async Task <IdentityResult> ValidateAsync(RoleManager <TRole> manager, TRole role) { if (manager == null) { throw new ArgumentNullEx(nameof(manager)); } if (role == null) { throw new ArgumentNullEx(nameof(role)); } var errors = new List <IdentityError>(); await ValidateRoleName(manager, role, errors); if (errors.Count > 0) { return(IdentityResult.Failed(errors.ToArray())); } return(IdentityResult.Success()); }
public virtual async Task <IdentityResult> SignUpAsync(string userName, string password) { var errors = new List <IdentityError>(); var existsUserName = await Repository.ExistsUserNameAsync(userName); if (existsUserName) { errors.Add(ErrorDescriber.UserNameIsExist()); return(IdentityResult.Fail(errors)); } var user = new User(password) { UserName = userName }; await Repository.AddAsync(user); return(IdentityResult.Success()); }
// 确保电子邮件不是空的、有效的、唯一的 public async Task <IdentityResult> ValidateEmail(IUserManager manager, User user, List <IdentityError> errors) { var email = user.Email; if (string.IsNullOrWhiteSpace(email)) { errors.Add(Describer.InvalidEmail(email)); return(IdentityResult.Failed(errors.ToArray())); } if (!new EmailAddressAttribute().IsValid(email)) { errors.Add(Describer.InvalidEmail(email)); return(IdentityResult.Failed(errors.ToArray())); } if (await manager.Users.AnyAsync(d => d.Email.Equals(email))) { errors.Add(Describer.DuplicateEmail(email)); return(IdentityResult.Failed(Describer.DuplicateEmail(email))); } return(IdentityResult.Success()); }
public async Task AddUserAsync() { var result = await UserManager.CreateAsync(new IdentityUser { UserName = "******", PhoneNumber = "18116699557", Email = "*****@*****.**" }, "Qwe@#23"); Assert.IsTrue(IdentityResult.Success().Succeeded); }