Asp.net Identity 2.0 - Unique Email

My MVC5 application, I tried to allow duplicate email address using the following:

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(); } 

By adding RequireUniqueEmail = false. This did not work, the page is redirected to the login page! My question is, can I only allow duplicate emails for this action and why am I redirected to the login page?

+6
source share
2 answers

Thanks Brendan, I moved

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

Before:

  var adminresult = await UserManager.CreateAsync(user); 
+4
source

If you use the default MVC 5 project template, the correct way to set the rules is in IdentityConfig.cs , and not 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>.... 
+20
source

Source: https://habr.com/ru/post/979751/


All Articles