简体   繁体   English

如何在ASP.Net MVC身份2中更改密码验证 - 第2部分

[英]How To Change Password Validation in ASP.Net MVC Identity 2 - Part 2

Before anyone suggests that this is the same question as the link below: 在任何人建议这与以下链接相同的问题之前:

How To Change Password Validation in ASP.Net MVC Identity 2? 如何在ASP.Net MVC Identity 2中更改密码验证?

I would like to assure people that it is more of a continuation of this question. 我想向人们保证,这更像是这个问题的延续。

I went into the IdentityConfig.cs as the first answer to the above link suggested and changed all the options as follows: 我进入IdentityConfig.cs作为建议的上述链接的第一个答案,并更改了所有选项,如下所示:

        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 2,
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = false,
            RequireUppercase = false,
        };

Next, as the second answer suggested I also went into the AccountViewModels.cs class and changed the 2 places there which sets the minimum length of the password. 接下来,如第二个答案所示,我也进入了AccountViewModels.cs类并更改了那里设置密码最小长度的2个位置。 I again set it from 6 to 2 (in both places it appears within the file. 我再次将它从6设置为2(在两个地方它都出现在文件中。

    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]

I then went ahead and done a search for the phrase "must be at least" throughout the entire solution. 然后我继续在整个解决方案中搜索“必须至少”这个短语。 I found one other class: ManageViewModels.cs and again in the two places this appeared I altered the minimum length setting it from 6 to 2 in both places, just as above. 我找到了另一个类:ManageViewModels.cs,再次在这两个地方出现了我改变了最小长度,在两个地方都设置了从6到2,就像上面一样。

I am trying to setup some default roles and a super user in the Startup.cs (Personally I would not do it this way, but I am just following an article on securing my CMS). 我试图在Startup.cs中设置一些默认角色和一个超级用户(我个人不会这样做,但我只是关注一篇关于保护我的CMS的文章)。 One of the first steps is this. 其中一个步骤就是这样。

In my Startup.cs I have the following simple code: 在我的Startup.cs中,我有以下简单的代码:

        //Here we create a Admin super user who will maintain the website                        
        var user = new ApplicationUser();
        user.UserName = "super";
        user.Email = "super@krypton.com";
        string userPWD = "1234";
        IdentityResult chkUser = UserManager.Create(user, userPWD);

        //Add default User to Role Admin
        if (chkUser.Succeeded)
        {
            IdentityResult result1 = UserManager.AddToRole(user.Id, "Admin");
        }

It should work. 它应该工作。 But when I break on it, and hover over the chkUser variable, I find that I am getting an error saying "The Password must be at least 6 characters long.". 但是当我打破它并将鼠标悬停在chkUser变量上时,我发现我收到一条错误消息“密码必须至少6个字符。”。

Even after changing all the validation logic to say it should be 2 characters. 即使在更改了所有验证逻辑后,也应该说它应该是2个字符。

So my question is: Is there another location where yet more validation logic is done? 所以我的问题是:还有另一个位置还有更多验证逻辑吗? Also, is there a single place where I can extract all this validation logic to (if so, can someone perhaps suggest a link or two to such a resource or provide a suggestion on how to approach it). 此外,是否有一个地方我可以提取所有这些验证逻辑(如果是这样,有人可能建议一个或两个这样的资源的链接或提供如何处理它的建议)。

The true and only link you can get is to the source code of PasswordValidator . 您可以获得的唯一链接是PasswordValidator的源代码。 Here you go: http://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/PasswordValidator.cs 您可以在这里: http//aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/PasswordValidator.cs

Only it is really doing what it is supposed to do. 只有它真正做它应该做的事情。 I can only guess that you assigning PasswordValidator to the wrong instance of UserManager and settings that you set are not used. 我只能猜测您将PasswordValidator分配给UserManager的错误实例,并且未使用您设置的设置。

What you can do is to copy the source code for PasswordValidator , paste it into your codebase and step-through in debug mode, figuring out where the requirement is coming from. 您可以做的是复制PasswordValidator的源代码,将其粘贴到您的代码库中,然后在调试模式中逐步执行,找出需求的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM