I have a .NetCore MVC APP with Identity and using this guide I was able to create custom user validators.public class UserDomainValidator<TUser> : IUserValidator<TUser> where TUser : IdentityUser{ private readonly List<string> _allowedDomains = new List<string> { "elanderson.net", "test.com" }; public Task<IdentityResult> ValidateAsync(UserManager<TUser> manager, TUser user) { if (_allowedDomains.Any(allowed => user.Email.EndsWith(allowed, StringComparison.CurrentCultureIgnoreCase))) { return Task.FromResult(IdentityResult.Success); } return Task.FromResult( IdentityResult.Failed(new IdentityError { Code = "InvalidDomain", Description = "Domain is invalid." })); }}并通过将其添加到DI中的我的身份服务中来成功验证我的用户创建and succesfully validate my User creation by adding it to my Identity service in DIservices.AddIdentity<ApplicationUser, IdentityRole>(options =>{ options.User.AllowedUserNameCharacters = "abccom."; options.User.RequireUniqueEmail = true;}) .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders() .AddUserValidator<UserDomainValidator<ApplicationUser>>();现在,其中之一身份中现有的验证人指出用户名必须是唯一的Now, one of the existing validatiors in Identity states that the username must be uniqueprivate async Task ValidateUserName(UserManager<TUser> manager, TUser user, ICollection<IdentityError> errors) { var userName = await manager.GetUserNameAsync(user); if (string.IsNullOrWhiteSpace(userName)) { errors.Add(Describer.InvalidUserName(userName)); } else if (!string.IsNullOrEmpty(manager.Options.User.AllowedUserNameCharacters) && userName.Any(c => !manager.Options.User.AllowedUserNameCharacters.Contains(c))) { errors.Add(Describer.InvalidUserName(userName)); } else { var owner = await manager.FindByNameAsync(userName); if (owner != null && !string.Equals(await manager.GetUserIdAsync(owner), await manager.GetUserIdAsync(user))) { errors.Add(Describer.DuplicateUserName(userName)); } } }由于在我的应用程序中,我的登录是通过租户+用户名/租户+电子邮件完成的,所以我想允许重复的用户名...是否有人做过类似的事情或有什么想法?Since in my app my login is done via Tenant + Username / Tenant + Email, I want to allow duplicated usernames... has anyone done something similar or have any ideas?我需要删除此验证,我想改用SignInManager或其他工具,以便它可以登录正确的用户.I need to remove this validation and I guess to adapt the SignInManager or something so it can sign in the correct user..推荐答案与其添加新的验证程序,不如替换服务中添加的验证程序,并创建自己的UserValidator.Instead of adding a new validator replace the one added in services, and create your own UserValidator. services.Replace(ServiceDescriptor.Scoped<IUserValidator<User>, CustomUserValidator<User>>()); public class CustomUserValidator<TUser> : IUserValidator<TUser> where TUser : class { private readonly List<string> _allowedDomains = new List<string> { "elanderson.net", "test.com" }; public CustomUserValidator(IdentityErrorDescriber errors = null) { Describer = errors ?? new IdentityErrorDescriber(); } public IdentityErrorDescriber Describer { get; } public virtual async Task<IdentityResult> ValidateAsync(UserManager<TUser> manager, TUser user) { if (manager == null) throw new ArgumentNullException(nameof(manager)); if (user == null) throw new ArgumentNullException(nameof(user)); var errors = new List<IdentityError>(); await ValidateUserName(manager, user, errors); if (manager.Options.User.RequireUniqueEmail) await ValidateEmail(manager, user, errors); return errors.Count > 0 ? IdentityResult.Failed(errors.ToArray()) : IdentityResult.Success; } private async Task ValidateUserName(UserManager<TUser> manager, TUser user, ICollection<IdentityError> errors) { var userName = await manager.GetUserNameAsync(user); if (string.IsNullOrWhiteSpace(userName)) errors.Add(Describer.InvalidUserName(userName)); else if (!string.IsNullOrEmpty(manager.Options.User.AllowedUserNameCharacters) && userName.Any(c => !manager.Options.User.AllowedUserNameCharacters.Contains(c))) { errors.Add(Describer.InvalidUserName(userName)); } } private async Task ValidateEmail(UserManager<TUser> manager, TUser user, List<IdentityError> errors) { var email = await manager.GetEmailAsync(user); if (string.IsNullOrWhiteSpace(email)) errors.Add(Describer.InvalidEmail(email)); else if (!new EmailAddressAttribute().IsValid(email)) { errors.Add(Describer.InvalidEmail(email)); } else if (_allowedDomains.Any(allowed => email.EndsWith(allowed, StringComparison.CurrentCultureIgnoreCase))) { errors.Add(new IdentityError { Code = "InvalidDomain", Description = "Domain is invalid." }); } else { var byEmailAsync = await manager.FindByEmailAsync(email); var flag = byEmailAsync != null; if (flag) { var a = await manager.GetUserIdAsync(byEmailAsync); flag = !string.Equals(a, await manager.GetUserIdAsync(user)); } if (!flag) return; errors.Add(Describer.DuplicateEmail(email)); } } } 这篇关于身份-自定义用户验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 16:22