6

私は現在、asp .netのカスタムメンバーシッププロバイダーを作成しています。私が抱えている問題は、標準のasp.netメンバーシッププロバイダーに提供するのと同じ方法でカスタムメンバーシッププロバイダーにパラメーターを提供する方法がわからないことです。パスワードの長さのようなweb.configファイル内。

4

3 に答える 3

5

MembershipProviderメソッドをオーバーライドする必要があるから独自のクラスを派生させる場合Initialize()、次のシグネチャがあります。

public override void Initialize(string name, NameValueCollection config);

は、ファイルSystem.Collections.NameValueCollectionに記述されているオプションを見つける辞書です。web.configこれらのオプションは、「標準」プロバイダーのオプションを(属性として)指定するのと同じ方法で提供されます。各ディクショナリエントリには、属性名のキーと、属性の値(as string)があります。

public  class MyMembershipProvider : MembershipProvider
{
    public override void Initialize(string name, NameValueCollection config)
    {
        base.Initialize(name, config);

        _enablePasswordReset = config.GetBoolean("enablePasswordReset", true);
    }
}

私の例でGetBoolean()は、次のようにどこかで宣言された拡張メソッドです。

public static bool GetBoolean(this NameValueCollection config,
    string valueName, bool? defaultValue)
{
    object obj = config[valueName];
    if (obj == null)
    {
        if (!defaultValue.HasValue)
            throw new WarningException("Required field has not been specified.");

        return defaultValue.Value;
    }

    bool value = defaultValue;
    if (obj is Boolean)
        return (bool)obj;

    IConvertible convertible = obj as IConvertible;
    try
    {
        return convertible.ToBoolean(CultureInfo.InvariantCulture);
    }
    catch (Exception)
    {
        if (!defaultValue.HasValue)
            throw new WarningException("Required field has invalid format.");

        return defaultValue.Value;
    }
}
于 2012-12-21T09:13:53.870 に答える
2

プロバイダーが派生している場合はMembershipProvider : ProviderBase、すべての構成をweb.configからロードして適用する必要があります。

IPrincipalカスタムおよび/またはを実装することを検討してくださいIIdentity-それは時々より良い拡張ポイントであり、誰もがそれを知っているわけではないので、それはしばしば使用されません。

于 2012-12-21T09:07:53.260 に答える
1

同じ方法で、標準の.netメンバーシップを定義します。

<membership defaultProvider="MyCustomMembershipProvider" userIsOnlineTimeWindow="30">
                <providers>
                    <clear />
                    <add name="MyCustomMembershipProvider" type="Namespace.MyCustomMembershipProvider" connectionStringName="db_ConnectionString" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false" passwordFormat="Encrypted" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
                    <add name="StandardMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="db_ConnectionString" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Encrypted" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
                </providers>
    </membership>
于 2012-12-21T09:15:11.943 に答える