Asp.net Identity 2.0 - Unique Email

自古美人都是妖i 提交于 2019-12-03 13:35:57

If you are using the default MVC 5 project template, the proper way is to set the rules in IdentityConfig.cs instead of in the registration controller.

Open App_Start\IdentityConfig.cs and edit this line:

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
{
    var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
    // Configure validation logic for usernames
    manager.UserValidator = new UserValidator<ApplicationUser>(manager)
    {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = false  //<-- the default is true
    };

    ....<snip>....

Thanks to Brendan, I moved

            UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager)
                {
                    RequireUniqueEmail = false
                };

Before:

 var adminresult = await UserManager.CreateAsync(user);

you have to add below lines in configureservices

 services.AddIdentity<ApplicationUser, IdentityRole>(options =>
            {
                options.User.RequireUniqueEmail = false;

            })
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!