我的应用程序是MVC5,我尝试使用以下内容允许重复的电子邮件地址:
public async Task<ActionResult> AddUser (UserRegisterViewModel userViewModel)
{
......
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
......
};
var adminresult = await UserManager.CreateAsync(user);
var result = await UserManager.AddToRolesAsync(user.Id, user.Profession);
UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager)
{
RequireUniqueEmail = false
};
if (adminresult.Succeeded)
{
....
return RedirectToAction("VisitInfo", "Visit");
}
if (!adminresult.Succeeded)
{
var er = adminresult.Errors.FirstOrDefault();
ViewBag.Error = er;
return View(userViewModel);
}
return RedirectToAction("VisitInfo", "Visit");
}
return View();
}
添加RequireUniqueEmail = false。它没有用,页面被重定向到登录页面!! 我的问题是,我是否可以仅为此操作允许重复的电子邮件,以及为什么我会被重定向到登录页面?
答案 0 :(得分:24)
如果您使用默认的MVC 5项目模板,正确的方法是在IdentityConfig.cs
而不是在注册控制器中设置规则。
打开App_Start\IdentityConfig.cs
并修改此行:
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>....
答案 1 :(得分:6)
感谢Brendan,我感动了
UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager)
{
RequireUniqueEmail = false
};
在:
var adminresult = await UserManager.CreateAsync(user);
答案 2 :(得分:3)
您必须在configureservices中添加以下行
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.User.RequireUniqueEmail = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();