2015-10-06 200 views
0

我想讓我的密碼字段接受空字符串。我正在使用數據庫第一種方法,並已更改AspNetUsers表的定義以允許PasswordHash爲空。但是在註冊期間它說password must be 6 characters。我如何刪除此驗證。我的項目不包含IdentityConfig.cs課程。我最近更新了Identity到2.2.1。我正在使用MVC。在asp.net身份更改密碼驗證

更新

我在寫AccountController這個功能。

[HttpPost] 
     [AllowAnonymous] 
     public async Task<JsonResult> RegisterUser(string email, string password = "") 
     { 
      var user = new ApplicationUser() { UserName = email }; 
      var result = await UserManager.CreateAsync(user, password); 
      if (result.Succeeded) 
      { 
       await SignInAsync(user, isPersistent: true); 
       return Json("Done", JsonRequestBehavior.AllowGet); 
      } 
      return Json("Error", JsonRequestBehavior.AllowGet); 

     } 

我在result變量中得到驗證錯誤。

+0

使用'MVC' ???? –

+0

是的,我正在使用MVC –

+0

您是否在Models文件夾中看到'AccountViewModel'類? –

回答

1

如果你看看:\ App_Start \ IdentityConfig.cs,在您看到 「創建」 的方法,RequiredLength = 6,你需要修改這個:

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

      // Configure validation logic for passwords 
      manager.PasswordValidator = new PasswordValidator 
      { 
       RequiredLength = 6, //This is the validation you need to modify. 
       RequireNonLetterOrDigit = true, 
       RequireDigit = true, 
       RequireLowercase = true, 
       RequireUppercase = true, 
      }; 

      // Configure user lockout defaults 
      manager.UserLockoutEnabledByDefault = true; 
      manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); 
      manager.MaxFailedAccessAttemptsBeforeLockout = 5; 

...

希望這個幫助你...

+0

我在quesiton提到,「我的項目不包含'IdentityConfig.cs'類」 –

+0

@IrfanWattoo是的,我知道了。但在這種情況下,是什麼的UserManager的類型,你的代碼嗎?如果它的類型是:「ApplicationUserManager」,那麼你可以F12,看看它在哪裏定義? –

+0

Step Into在該行不起作用,而是顯示一個彈出消息並執行一個步驟。但是當我將鼠標懸停在UserManager上時,彈出窗口是:'Microsoft.aspnet.identity.UserManager ' –

0

@Irfan Yousanif,因爲你沒有IdentityConfig.cs類,你可以按照以下的示例場景我的示例代碼寫:

var userStore = new UserStore<IdentityUser>(); 
     var manager = new UserManager<IdentityUser>(userStore); 

     manager.PasswordValidator = new PasswordValidator 
     { 
      RequiredLength = 8, 
      RequireNonLetterOrDigit = true, 
      RequireDigit = true, 
      RequireLowercase = true, 
      RequireUppercase = true, 
     }; 


     var user = new IdentityUser() { UserName = UserName.Text }; 
     IdentityResult result = manager.Create(user, Password.Text); 

,如果你想刪除整個驗證,請評論如下:

manager.PasswordValidator = new PasswordValidator 
     { 
      //RequiredLength = 8, 
      //RequireNonLetterOrDigit = true, 
      //RequireDigit = true, 
      //RequireLowercase = true, 
      //RequireUppercase = true, 
     };