model-view-controller - 如何在自定义MembershipProvider上调用Initialize?

标签 model-view-controller asp.net-membership web-config

我已经阅读了所有相关问题,但是由于某种原因,我仍然无法找到正确的解决方案,这是我不正确的事情,但是不确定是什么原因造成的。

我创建了一个自定义成员资格提供程序,也将我的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,则不必做任何东西,但是我确实尝试手动调用它,但是当我尝试转换NameValueCollection时,它给了我InvalidCastException。

谁能帮我?谢谢

最佳答案

为了调用Initialize(),您将需要以某种方式实例化自定义成员资格提供程序。像这样:

MyCustomMembershipProvider myProvider = (MyCustomMembershipProvider)Membership.Providers["NameOfMembershipProviderInConfig"];

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

关于model-view-controller - 如何在自定义MembershipProvider上调用Initialize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/623545/

相关文章:

iphone - 带有 ARC 的 MVC 模式

c# - 使用数据库保存应用程序设置

asp.net - 使用 web.config 拒绝访问网页

.net - ASP.NET:跨应用程序的表单例份验证:匹配密码加密设置

visual-studio-2010 - SQL Server 角色提供程序单元测试期间出现 "The role manager feature has not been enabled"错误

c# - 使用 Web.config 在 C# Asp.Net 中显示错误信息?

ios - 需要帮助以更好地了解Objective C中的MVC

java - Spring MVC 4.1.7 类级别@RequestMapping

ios - Apple 是否违反了 iOS 8 上 MailboxContentViewCell.h 中的 MVC 规则?

asp.net - 从 ASP.NET 2.0 成员身份解密 'Encrypted' 密码