模型视图控制器 – 如何在自定义MembershipProvider上调用Initia
发布时间:2020-12-16 00:42:13 所属栏目:asp.Net 来源:网络整理
导读:我已经阅读了所有相关的问题,但是由于某些原因我仍然无法得到正确的解决方案,但是有些事情并不在我身边,但不能确定是什么原因造成的。 我已经创建了一个自定义会员提供商,也将我的web.config更改为: membership defaultProvider="MyMemberShipProvider"
我已经阅读了所有相关的问题,但是由于某些原因我仍然无法得到正确的解决方案,但是有些事情并不在我身边,但不能确定是什么原因造成的。
我已经创建了一个自定义会员提供商,也将我的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()。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- asp.net-mvc – 如何测试MVC控制器的事件
- asp.net-mvc – ASP.net MVC – 视图如何访问模型
- asp.net-mvc – 在自定义编辑器中只包含一次脚本
- ASP.NET MVC发布模型ID的更好方法是什么?
- asp.net – 使用Mono的Linux上的ActiveX?
- asp.net-mvc – 如何使ASP.NET路由转义路由值?
- 无法在ASP.NET VB站点中加载System.DirectorySer
- asp.net-web-api – SignalR响应覆盖标头
- ASP.NET FileUpload – 如何更改“浏览…”按钮说
- asp.net-core – 如何在ApplicationStarted事件中
热点阅读