使用web.config在自定义成员资格中设置minRequiredPasswordLength

时间:2013-10-11 13:41:11

标签: c# asp.net web-config membership-provider

我在项目中有 CustomMembership 类,并在课堂上使用此代码:

private static int _MinRequiredPasswordLength;
public static int MinRequiredPasswordLength
    {
        set { _MinRequiredPasswordLength = value; }
        get { return _MinRequiredPasswordLength; }
    }

并在web.config中设置成员资格

<membership defaultProvider="CustomMembership">
  <providers>
    <clear />
    <add name="CustomMembership" type="Project1.Code.CustomMembership, Project1, Version=1.0.0.0, Culture=neutral" connectionStringName="PConn" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>

并使用此代码获取minRequiredPasswordLength:

MinRequiredPasswordLength.Text = CMembership.MinRequiredPasswordLength.ToString();

但得到'0'!我想要'6'。

2 个答案:

答案 0 :(得分:2)

如果您是从抽象MembershipProvider类派生的,则应覆盖MembershipProvider.Initializeconfig集合将包含所有配置属性,您可以使用它们来设置属性,包括MinRequiredPasswordLength

private int _minRequiredPasswordLength;

public override void Initialize(string name, NameValueCollection config)
{
    _minRequiredPasswordLength = // get it from config["minRequiredPasswordLength"], with validation and conversion to int.
}

public override MinRequiredPasswordLength
{
    get { return _minRequiredPasswordLength; }
}

如果您来自现有提供商,例如SqlMembershipProvider,然后您可以简单地使用MinRequiredPasswordLength的基类实现。

答案 1 :(得分:1)

尝试:

MinRequiredPasswordLength.Text = @CustomMembership.MinRequiredPasswordLength