简体   繁体   中英

ASP.NET Core 2.2 How to override UserManager.CreateAsync to remove unique Username requirement

I am trying to override my UserManager.CreateAsync to remove the requirement of a unique Username. How can I do that?

So far this is what I have

public class CustomUserManager : UserManager<User>
{
    public string TenantId { get; set; }
    public CustomUserManager(IUserStore<User> store, IOptions<IdentityOptions> optionsAccessor,
    IPasswordHasher<User> passwordHasher, IEnumerable<IUserValidator<User>> userValidators,
    IEnumerable<IPasswordValidator<User>> passwordValidators, ILookupNormalizer keyNormalizer,
    IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<User>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
    {
    }

    public override async Task<IdentityResult> CreateAsync(User user, string Password)
    {
        ThrowIfDisposed();
        user.TenantId = this.TenantId; 
        if (user == null)
        {
            throw new ArgumentNullException("user");
        }
        var result = dbContext.Users.Any(u => u.UserName == user.UserName && u.TenantId == user.TenantId);
        if (result == true)
        {
            return IdentityResult.Failed();
        }
        return await base.CreateAsync(user, Password);
    }
}

I can't force create the user due to me using base.CreateAsync. How can I overwrite what CreateAsync is doing?

I am stuck with this.

Please try to set dbContext.Configuration.ValidateOnSaveEnabled = false; see if it's working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM