2013-11-01 16 views
13

Cố gắng thay đổi độ dài mật khẩu tối thiểu mặc định thành 4 ký tự. Tôi biết, 4 !!! Vô lý, đúng! Không phải cuộc gọi của tôi.Thay đổi độ dài mật khẩu trong MVC 5 Tư cách thành viên

Dù sao, tôi đã thay đổi nó trên RegisterViewModel nhưng điều đó không thực sự thay đổi nó. Để minh họa cho tôi đã đăng mã dưới đây. ModleState.IsValid trả về chính xác dựa trên ViewModel được cập nhật. Tuy nhiên, sau đó gọi số UserManager.CreateAsync() trả về False với thông báo lỗi "Mật khẩu phải có ít nhất 6 ký tự"

Tôi đã làm theo các bước này, rất giống bài (Change Password...) nhưng không hoạt động cho MVC 5 như tôi có thể nói. Nó vẫn trả về cùng một thông điệp.

// 
    // POST: /Account/Register 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser() { UserName = model.UserName, LastLogin = model.LastLogin }; 


// This is where it 'fails' on the CreateAsync() call 
        var result = await UserManager.CreateAsync(user, model.Password); 
        if (result.Succeeded) 
        { 
         await SignInAsync(user, isPersistent: false); 
         return RedirectToAction("Index", "Home"); 
        } 
        else 
        { 
         AddErrors(result); 
        } 
       } 
      // If we got this far, something failed, redisplay form 
      return View(model); 
     } 

Trả lời

14

Như bạn thấy UserManager có tài sản công cộng IIdentityValidator<string> PasswordValidator để xác nhận mật khẩu mà hiện nay được khởi tạo trong constructor UserManager 's với tham số mã hóa cứng this.PasswordValidator = (IIdentityValidator<string>) new MinimumLengthValidator(6);.

Bạn có thể đặt thuộc tính này với đối tượng MinimumLengthValidator với độ dài mật khẩu bắt buộc.

4

Kiểm tra bài viết sau tại MSDN

Implementing custom password policy using ASP.NET Identity

Các gợi ý ở đây là để mở rộng các lớp UserManager trong ứng dụng và thiết lập PasswordValidator tài sản trong contructor:

public class MyUserManager : UserManager<ApplicationUser> 
{ 
    public MyUserManager() : 
     base(new UserStore<ApplicationUser>(new ApplicationDbContext())) 
    { 
     PasswordValidator = new MinimumLengthValidator(4); 
    } 
} 

Và sau đó trong bộ điều khiển của bạn (hoặc lớp cơ sở bộ điều khiển) nhanh chóng MyUserManager:

public BaseController() : this(new MyUserManager()) 
{ 
} 

public BaseController(MyUserManager userManager) 
{ 
    UserManager = userManager; 
} 

public MyUserManager UserManager { get; private set; } 

Bạn cũng có thể triển khai trình xác thực tùy chỉnh để kiểm tra các quy tắc mật khẩu phức tạp hơn bằng cách triển khai IIdentityValidator và thay thế trình xác thực mặc định.

9

Bạn có thể đặt thuộc tính mật khẩu bằng PasswordValidator được tìm thấy trong tệp IdentityConfig.cs trong Thư mục App_Start.

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, 
      RequireNonLetterOrDigit = false, 
      RequireDigit = true, 
      RequireLowercase = true, 
      RequireUppercase = true, 
     }; 

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

     // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user 
     // You can write your own provider and plug it in here. 
     manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser> 
     { 
      MessageFormat = "Your security code is {0}" 
     }); 
     manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> 
     { 
      Subject = "Security Code", 
      BodyFormat = "Your security code is {0}" 
     }); 
     manager.EmailService = new EmailService(); 
     manager.SmsService = new SmsService(); 
     var dataProtectionProvider = options.DataProtectionProvider; 
     if (dataProtectionProvider != null) 
     { 
      manager.UserTokenProvider = 
       new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); 
     } 
     return manager; 
    } 
Các vấn đề liên quan