I develope ASP.NEW MVC5 application (not ASP 5). I want to allow users to change email later. For this cause I do not want user names to be equal to emails. I do it like this (by generating username):

var user = new ApplicationUser { UserName = GenerateUserName(), Email = model.Email };

At the same time I want emails to be unique, so I set RequireUniqueEmail = true in IdentityConfig.cs:

            manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true

The question is: why does UserManager successfully create new user with already existing email (not user name!)?

var result = await UserManager.CreateAsync(user, model.Password);

My guess is, that it checks for UserName uniqueness (which in my case is provided by GenerateUserName() method). If my guess is correct - RequireUniqueEmail property is mistakingly named so, it probably should be named RequireUniqueUserName. Or there's other reason I don't know of. Please, help.

Related posts

Recent Viewed