2016-06-28 71 views
0

我正在使用asp.Identity在我的應用程序中執行用戶和角色模塊。我創建用戶喜歡這個Asp.NET身份問題

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

它創建的用戶,問題是,在應用程序管理器不檢查重複的電子郵件。我的應用程序管理器看起來像這樣

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

另一個問題是,如果我登錄使用的用戶名它的工作原理,但如果我使用EMAL返回null。

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

任何人都熟悉這個問題?

+0

假設如果'result1'不成功,那麼你應該有一個'ModelState.AddModelError(「」,「無效的登錄嘗試。」);' –

+0

,但它成功並創建用戶,即使電子郵件不是獨特 – mohsinali1317

+0

爲您的電子郵件登錄問題我建議你看看[這個](http://stackoverflow.com/questions/27498840/how-to-login-using-email-in-identity-2) –

回答

1

ApplicationUserManager.AppUserManager.Create,因爲你不是指ApplicationUserManager方面,它是這樣並不驗證電子郵件:

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) 

上面的例子VAR manager將包含ApplicationUserManager背景和電子郵件的審定會由RequireUniqueEmail = true完成。