0

I am using asp.Identity to do the users and roles module in my application. I create user like this

var user = new ApplicationUser() { UserName = name, Email = email };
IdentityResult result1 = ApplicationUserManager.AppUserManager.Create(user, password);

It creates the user, the issue is that in the Application Manager it doesn't check for duplicate email. My application manager looks like this

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var manager = new ApplicationUserManager(new EntityUserStore<ApplicationUser, Account, ApplicationRole, Role>());

        AppUserManager = manager;

        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = false,
            RequireUppercase = false,
        };
        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
}

The other is issue that, if I login using user name it works but if I use emal it returns null.

 ApplicationUser user = UserManager.FindByEmail(email); // this returns null

Anyone familiar with this issue?

mohsinali1317
  • 4,255
  • 9
  • 46
  • 85
  • Supposedly if `result1` is Not Succeeded then you should have a `ModelState.AddModelError("", "Invalid login attempt.");` – Shawn Yan Jun 28 '16 at 09:04
  • but it is succeeded and the user is created, even when the email is not unique – mohsinali1317 Jun 28 '16 at 09:07
  • For your issue of login with email I suggest you to look into [this](http://stackoverflow.com/questions/27498840/how-to-login-using-email-in-identity-2) – Shawn Yan Jun 28 '16 at 09:12
  • May i know why do you need `AppUserManager`? – Shawn Yan Jun 28 '16 at 09:20
  • I don't get the question, I mean isn't that the way to do it? I have been following some tutorials and that is how they do it. – mohsinali1317 Jun 28 '16 at 09:33
  • Because from what I can see , your `AppUserManager` does nothing other than getting assigned with `manager`.When you create user,it should be like this: `Dim result = Await UserManager.CreateAsync(user, model.Password) If result.Succeeded Then` – Shawn Yan Jun 28 '16 at 09:36
  • I would assume that since your are doing `ApplicationUserManager.AppUserManager.Create` the user gets created(strangely) and didn't check for duplicate email probably because `ApplicationUserManager.AppUserManager.Create` is not same with `UserManager.CreateAsync` and so your validator `RequireUniqueEmail = true` didn't work. – Shawn Yan Jun 28 '16 at 09:40
  • oh okay. I will try the Async one. – mohsinali1317 Jun 28 '16 at 09:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115822/discussion-between-shawn-yan-and-chaudhry-mohsin-ali). – Shawn Yan Jun 28 '16 at 09:41

1 Answers1

1

Your ApplicationUserManager.AppUserManager.Create does not validate the email because you are not referring to the ApplicationUserManager context,which is something like this:

var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>(); 
var user = new ApplicationUser() { UserName = name, Email = email }; 
IdentityResult result = manager.Create(user, password); 
if (result.Succeeded)

The above example var manager will contain the ApplicationUserManager context and validation of email will be done by RequireUniqueEmail = true.

Shawn Yan
  • 183
  • 8