我在项目中有 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'。
答案 0 :(得分: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
的基类实现。
答案 1 :(得分:1)
尝试:
MinRequiredPasswordLength.Text = @CustomMembership.MinRequiredPasswordLength