在MVC 5 Membership中更改密码长度

Ref*_*din 14 .net simplemembership asp.net-mvc-5 asp.net-identity

尝试将默认最小密码长度更改为4个字符.我知道,4 !!! 太荒谬了吧!不是我的电话.

无论如何,我已经改变了它,RegisterViewModel但实际上并没有改变它.为了说明我已经发布了以下代码.该ModleState.IsValid收益的基础上正确更新视图模型.然而,它然后调用UserManager.CreateAsync()哪个返回False错误消息"密码必须至少6个字符"

我已经按照这个非常相似的帖子(更改密码......)中的步骤进行了操作,但就我所知,它对MVC 5不起作用.它仍然返回相同的消息.

//
    // 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);
        }
Run Code Online (Sandbox Code Playgroud)

小智 15

正如您所看到的,UserManager具有IIdentityValidator<string> PasswordValidator用于密码验证的公共属性,该属性当前在UserManager具有硬编码参数的构造函数中初始化this.PasswordValidator = (IIdentityValidator<string>) new MinimumLengthValidator(6);.

您可以使用MinimumLengthValidator具有所需密码长度的对象设置此属性.


Tar*_*zan 9

您可以使用App_Start目录中IdentityConfig.cs文件中的PasswordValidator设置密码属性.

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;
    }
Run Code Online (Sandbox Code Playgroud)