简体   繁体   English

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

[英]set minRequiredPasswordLength in custom membership with web.config

i have CustomMembership class in project and use this code in class: 我在项目中有CustomMembership类,并在类中使用此代码:

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

and set membership in web.config 并在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>

and use this code for get minRequiredPasswordLength: 并使用此代码获取minRequiredPasswordLength:

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

but get '0'! 但是得到“ 0”! I want get '6'. 我想得到“ 6”。

If you've derived from the abstract MembershipProvider class, you should override MembershipProvider.Initialize . 如果从抽象的MembershipProvider类派生,则应重写MembershipProvider.Initialize The config collection will contain all the configuration attributes, and you can use them to set your properties including MinRequiredPasswordLength : 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; }
}

If you've derived from an existing provider, eg SqlMembershipProvider , then you can simply use the base class implementation of MinRequiredPasswordLength . 如果您是从现有提供程序(例如SqlMembershipProvider派生的,则可以简单地使用MinRequiredPasswordLength的基类实现。

尝试:

MinRequiredPasswordLength.Text = @CustomMembership.MinRequiredPasswordLength

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

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