SimpleMembershipProvider MinRequiredPasswordLength始终返回0,尽管它在Web.config成员资格提供程序部分中被定义为6

时间:2012-10-03 05:59:46

标签: web-config asp.net-membership asp.net-mvc-4 settings simplemembership

我在ASP.NET MVC 4应用程序中切换到SimpleMembershipProvider。其他一切都很好但是有这个问题......

当我尝试这段代码时:

var password = Membership.GeneratePassword(Membership.MinRequiredPasswordLength, 0);

MinRequiredPasswordLength始终为0.会员提供商的Web.config部分中定义的设置未被阅读。

以下是会员默认提供商Web.config部分:

<membership defaultProvider="AspNetSqlMembershipProvider">
    <providers>
         <clear />
             <add name="AspNetSqlMembershipProvider"
                  type="System.Web.Security.SqlMembershipProvider"
                  connectionStringName="DefaultConnection"
                  enablePasswordRetrieval="false"
                  enablePasswordReset="true"
                  requiresQuestionAndAnswer="false"
                  requiresUniqueEmail="false"
                  maxInvalidPasswordAttempts="5"
                  minRequiredPasswordLength="6"
                  minRequiredNonalphanumericCharacters="0"
                  passwordAttemptWindow="10"
                  applicationName="/Acad" />
    </providers>
</membership>

这是我得到的调试信息:

enter image description here

任何提示?

SimpleMembershipProvider是否如此简单以至于它甚至不使用Web.config部分?

目前的解决方案

为了克服SimpleMembershipProvider当前限制,我使用this code来获取Web.config中定义的值:

MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");
var defaultProvider = membershipSection.DefaultProvider;
ProviderSettings providerSettings = membershipSection.Providers[defaultProvider];
var minRequiredPasswordLength = int.Parse(providerSettings.Parameters["minRequiredPasswordLength"]);

3 个答案:

答案 0 :(得分:3)

  

我在ASP.NET MVC 4应用程序中切换到SimpleMembershipProvider。

嗯,从您在web.config中显示的相关部分来看,这似乎根本就不是这样。您正在使用AspNetSqlMembershipProvider

如果您想切换到SimpleMembershipProvider,请确保您已正确配置它:

<membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <clear/>
    <add name="SimpleMembershipProvider"
         type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"
    />
  </providers>
</membership>

据说SimpleMembershipProvider不支持通过web.config设置这些属性。它甚至没有使用它。如果你想要这样的功能,你必须自己实现它。

答案 1 :(得分:3)

您可以继承SimpleMembershipProvider并覆盖MinRequiredPasswordLength

public class YoursSimpleMembershipProvider : SimpleMembershipProvider
{
    private const int MIN_REQUIRED_PASSWORD_LENGTH = 6;

    private const int MIN_REQUIRED_NON_ALPHANUMERIC_CHARACTERS = 0;

    public override int MinRequiredPasswordLength
    {
        get
        {
            return MIN_REQUIRED_PASSWORD_LENGTH;
        }
    }
}

答案 2 :(得分:0)

我发现我在web.config中放了minRequiredPasswordLength =“3”,如下所示:

<membership defaultProvider="SimpleMembershipProvider" >
  <providers>
    <clear/>
    <add name="SimpleMembershipProvider" 
         type="WebMatrix.WebData.SimpleMembershipProvider,WebMatrix.WebData"
         minRequiredPasswordLength="3"
    />
  </providers>     
</membership>

没有效果。 我在MODEL中找到了秘密。如果你创建一个Internet应用程序模板,在AccountModels.cs中有一个“RegisterModel”,就像这样:

    public class RegisterModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "确认密码")]
        [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }

在Password属性中有一个StringLength-&gt; MinimumLength。 我想是的!

SimpleMembershipProvider中删除了一些功能,请参见此处:http://sharifhkhan.com/programming/features-removed-in-simplemembershipprovider/

希望这对你有所帮助。