更改asp.net身份

时间:2015-10-06 08:05:00

标签: asp.net asp.net-identity

我希望我的密码字段接受空字符串。我正在使用数据库第一种方法,我已经更改了AspNetUsers表的定义,以允许PasswordHash为空。但在注册期间它说password must be 6 characters。如何删除此验证。我的项目不包含IdentityConfig.cs类。我最近更新了Identity到2.2.1。我正在使用MVC。

更新

我已在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变量中收到验证错误。

2 个答案:

答案 0 :(得分:1)

If you look at: \App_Start\IdentityConfig.cs, in the "Create" method you see, RequiredLength = 6 and you need to modify this:

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;

...

Hope this help you...

答案 1 :(得分: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,
        };