模型视图控制器 – 如何在自定义MembershipProvider上调用Initialize?

前端之家收集整理的这篇文章主要介绍了模型视图控制器 – 如何在自定义MembershipProvider上调用Initialize?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经阅读了所有相关的问题,但是由于某些原因我仍然无法得到正确的解决方案,但是有些事情并不在我身边,但不能确定是什么原因造成的。

我已经创建了一个自定义会员提供商,也将我的web.config更改为:

<membership defaultProvider="MyMemberShipProvider">
      <providers>
        <clear />
        <add name="MyMemberShipProvider" 
                  type="MyNameSpace.MyMemberShipProvider" 
                  connectionStringName="ApplicationServices" 
                  enablePasswordRetrieval="false" 
                  enablePasswordReset="true" 
                  requiresQuestionAndAnswer="false" 
                  requiresUniqueEmail="false" 
                  passwordFormat="Hashed" 
                  maxInvalidPasswordAttempts="5" 
                  minrequiredPasswordLength="6" 
                  minrequiredNonalphanumericCharacters="0" 
                  passwordAttemptWindow="10" 
                  passwordStrengthRegularExpression="" 
                  applicationName="MyApplication" />
      </providers>
    </membership>

这是我的Initialize方法代码

public override void Initialize(string name,NameValueCollection config)
{
    if (config == null)
    { throw new ArgumentNullException("config"); }

    if (string.IsNullOrEmpty(name))
    { name = "MyMemberShipProvider"; }

    if (string.IsNullOrEmpty(config["description"]))
    {
        config.Remove("description");
        config.Add("description","My Membership Provider");
    }

    base.Initialize(name,config);

    _applicationName = GetConfigValue(config["applicationName"],System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
    _maxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"],"5"));
    _passwordAttemptWindow = Convert.ToInt32(GetConfigValue(config["passwordAttemptWindow"],"10"));
    _minrequiredNonAlphaNumericCharacters = Convert.ToInt32(GetConfigValue(config["minrequiredAlphaNumericCharacters"],"1"));
    _minrequiredPasswordLength = Convert.ToInt32(GetConfigValue(config["minrequiredPasswordLength"],"7"));
    _passwordStregthRegularExpression = Convert.ToString(GetConfigValue(config["passwordStrengthRegularExpression"],String.Empty));
    _enablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"],"true"));
    _enablePasswordRetrieval = Convert.ToBoolean(GetConfigValue(config["enablePasswordRetrieval"],"true"));
    _requiredQuestionAndAnswer = Convert.ToBoolean(GetConfigValue(config["requiresQuestionAndAnswer"],"false"));
    _requiredUniqueEmail = Convert.ToBoolean(GetConfigValue(config["requiresUniqueEmail"],"true"));

    string temp_format = config["passwordFormat"];
    if (temp_format == null)
    {
        temp_format = "Hashed";
    }

    switch (temp_format)
    {
        case "Hashed":
            _passwordFormat = MembershipPasswordFormat.Hashed;
            break;
        case "Encrypted":
            _passwordFormat = MembershipPasswordFormat.Encrypted;
            break;
        case "Clear":
            _passwordFormat = MembershipPasswordFormat.Clear;
            break;
        default:
            throw new ProviderException("Password format not supported.");
    }

    ConnectionStringSettings _connectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];

    if (_connectionStringSettings == null || _connectionStringSettings.ConnectionString.Length == 0)
    {
        throw new ProviderException("Connection String Cannot Be Blank.");
    }

    _connectionString = _connectionStringSettings.ConnectionString;

    //Get Encryption and Decryption Key Information From the Information.

    System.Configuration.Configuration cfg = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
    _machinekey = cfg.GetSection("system.web/machineKey") as MachineKeySection;

    if (_machinekey.ValidationKey.Contains("AutoGenerate"))
    {
        if (PasswordFormat != MembershipPasswordFormat.Clear)
        {
            throw new ProviderException("Hashed or Encrypted passwords are not supported with auto-generated keys.");
        }
    }

}

我已经注意到,Initialize方法没有被调用,我读过这里的问题,人们说我不用手动调用,如果我正确地连接了web.config,我不必做任何东西,但我尝试手动调用,但它给我一个InvalidCastException当我试图转换NameValueCollection。

有人可以帮我吗谢谢

解决方法

对于要调用的Initialize(),您需要以某种方式实例化您的自定义成员资格提供程序。像这样:
MyCustomMembershipProvider myProvider = (MyCustomMembershipProvider)Membership.Providers["NameOfMembershipProviderInConfig"];

现在,当您使用myProvider时,将调用您的自定义提供程序的Initialize()。

原文链接:https://www.f2er.com/aspnet/252971.html

猜你在找的asp.Net相关文章