0

我在项目中有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'。

4

2 回答 2

2

如果你是从抽象MembershipProvider类派生的,你应该重写MembershipProvider.Initialize. 该config集合将包含所有配置属性,您可以使用它们来设置您的属性,包括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

于 2013-10-11T17:24:04.980 回答
1

尝试 :

MinRequiredPasswordLength.Text = @CustomMembership.MinRequiredPasswordLength
于 2013-10-11T13:43:40.980 回答