Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

RequireUniqueEmail when set to false is not working #1061


User avatar
0
omar created

I would like to create new users with no emails. Inside my UserManager's constructor, I have the following code:

UserValidator = new UserValidator<User, long>(this)
            {
                 RequireUniqueEmail = false,                
             };

However, I am still getting a validation error with an empty email address.


3 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    EmailAddress is required for Abp.Zero. I suggest you to use it, since UserManager checks it on login. You can change required attribute by fluent mapping configuration in onmodelcreating, this may break login as I said. If you will not show it at all, put a <a href="mailto:[email protected]">[email protected]</a> for uniqueness.

  • User Avatar
    0
    omar created

    Thank you for your help. In case anyone need it.

    class User

    [Required(AllowEmptyStrings=true)]
            public override string EmailAddress { get; set; }
    

    UserManager

    public override async Task<IdentityResult> CheckDuplicateUsernameOrEmailAddressAsync(long? expectedUserId, string userName, string emailAddress)
            {
                var user = (await FindByNameAsync(userName));
                if (user != null && user.Id != expectedUserId)
                {
                    return AbpIdentityResult.Failed(string.Format("Name {0} is already taken.", userName));
                }
    
                // if email  address was provided, check that it does not exist
                if (!string.IsNullOrEmpty(emailAddress))
                {
                    user = (await FindByEmailAsync(emailAddress));
                    if (user != null && user.Id != expectedUserId)
                    {
                        return AbpIdentityResult.Failed(string.Format("Email Address {0} is already taken.", emailAddress));
                    }
                }
    
                return IdentityResult.Success;
            }
    

    You mentioned that "this may break login". I was going over the AbpUserManager.cs class, but I was not able to find a problem since the LoginAsyncInternal function use the username or email to login. Any idea where this will break? Thanks again

  • User Avatar
    0
    hikalkan created
    Support Team

    Since all emails will be empty (same), and login method checks also emails, it may be problem I thought. If not, never mind it.