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

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

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

  1. <membership defaultProvider="MyMemberShipProvider">
  2. <providers>
  3. <clear />
  4. <add name="MyMemberShipProvider"
  5. type="MyNameSpace.MyMemberShipProvider"
  6. connectionStringName="ApplicationServices"
  7. enablePasswordRetrieval="false"
  8. enablePasswordReset="true"
  9. requiresQuestionAndAnswer="false"
  10. requiresUniqueEmail="false"
  11. passwordFormat="Hashed"
  12. maxInvalidPasswordAttempts="5"
  13. minrequiredPasswordLength="6"
  14. minrequiredNonalphanumericCharacters="0"
  15. passwordAttemptWindow="10"
  16. passwordStrengthRegularExpression=""
  17. applicationName="MyApplication" />
  18. </providers>
  19. </membership>

这是我的Initialize方法代码

  1. public override void Initialize(string name,NameValueCollection config)
  2. {
  3. if (config == null)
  4. { throw new ArgumentNullException("config"); }
  5.  
  6. if (string.IsNullOrEmpty(name))
  7. { name = "MyMemberShipProvider"; }
  8.  
  9. if (string.IsNullOrEmpty(config["description"]))
  10. {
  11. config.Remove("description");
  12. config.Add("description","My Membership Provider");
  13. }
  14.  
  15. base.Initialize(name,config);
  16.  
  17. _applicationName = GetConfigValue(config["applicationName"],System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
  18. _maxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"],"5"));
  19. _passwordAttemptWindow = Convert.ToInt32(GetConfigValue(config["passwordAttemptWindow"],"10"));
  20. _minrequiredNonAlphaNumericCharacters = Convert.ToInt32(GetConfigValue(config["minrequiredAlphaNumericCharacters"],"1"));
  21. _minrequiredPasswordLength = Convert.ToInt32(GetConfigValue(config["minrequiredPasswordLength"],"7"));
  22. _passwordStregthRegularExpression = Convert.ToString(GetConfigValue(config["passwordStrengthRegularExpression"],String.Empty));
  23. _enablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"],"true"));
  24. _enablePasswordRetrieval = Convert.ToBoolean(GetConfigValue(config["enablePasswordRetrieval"],"true"));
  25. _requiredQuestionAndAnswer = Convert.ToBoolean(GetConfigValue(config["requiresQuestionAndAnswer"],"false"));
  26. _requiredUniqueEmail = Convert.ToBoolean(GetConfigValue(config["requiresUniqueEmail"],"true"));
  27.  
  28. string temp_format = config["passwordFormat"];
  29. if (temp_format == null)
  30. {
  31. temp_format = "Hashed";
  32. }
  33.  
  34. switch (temp_format)
  35. {
  36. case "Hashed":
  37. _passwordFormat = MembershipPasswordFormat.Hashed;
  38. break;
  39. case "Encrypted":
  40. _passwordFormat = MembershipPasswordFormat.Encrypted;
  41. break;
  42. case "Clear":
  43. _passwordFormat = MembershipPasswordFormat.Clear;
  44. break;
  45. default:
  46. throw new ProviderException("Password format not supported.");
  47. }
  48.  
  49. ConnectionStringSettings _connectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
  50.  
  51. if (_connectionStringSettings == null || _connectionStringSettings.ConnectionString.Length == 0)
  52. {
  53. throw new ProviderException("Connection String Cannot Be Blank.");
  54. }
  55.  
  56. _connectionString = _connectionStringSettings.ConnectionString;
  57.  
  58. //Get Encryption and Decryption Key Information From the Information.
  59.  
  60. System.Configuration.Configuration cfg = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
  61. _machinekey = cfg.GetSection("system.web/machineKey") as MachineKeySection;
  62.  
  63. if (_machinekey.ValidationKey.Contains("AutoGenerate"))
  64. {
  65. if (PasswordFormat != MembershipPasswordFormat.Clear)
  66. {
  67. throw new ProviderException("Hashed or Encrypted passwords are not supported with auto-generated keys.");
  68. }
  69. }
  70.  
  71. }

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

有人可以帮我吗谢谢

解决方法

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

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

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