模型视图控制器 – 如何在自定义MembershipProvider上调用Initialize?
发布时间:2020-05-23 15:35:21 所属栏目:asp.Net 来源:互联网
导读:我已经阅读了所有相关的问题,但是由于某些原因我仍然无法得到正确的解决方案,但是有些事情并不在我身边,但不能确定是什么原因造成的。 我已经创建了一个自定义会员提供商,也将我的web.config更改为: membership defaultProvider=MyMemberShipProvider pr
|
我已经阅读了所有相关的问题,但是由于某些原因我仍然无法得到正确的解决方案,但是有些事情并不在我身边,但不能确定是什么原因造成的。 我已经创建了一个自定义会员提供商,也将我的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-4 – MVC 4中的HttpContext.Current.Request.I
- asp.net-mvc-3 – MVC 3客户端验证,模型绑定十进制值和文化
- 压力测试ASP.Net应用程序
- asp.net-mvc – 用静态项绑定Html.DropDownList
- ELMAH在ASP.NET vNext?
- asp.net – SignalR并不总是准备好后start().done()?
- asp.net-mvc – 将viewdata传递给asp.net mvc masterpages
- asp.net – 如何添加.aspx页面到现有的MVC 4项目?
- asp.net – 当使用AngularJS,WebAPI 2和Oauth 2时,如何将授
- asp.net – 剑道:网格中的ComboBox – 将选定组合框的其他
推荐文章
站长推荐
- asp.net-mvc – 获取文件内容的绝对路径
- asp.net – 有没有办法禁用整个页面的事件验证?
- asp.net-mvc-4 – 如何在asp.net mvc4中将OpenID
- asp.net – 当使用AngularJS,WebAPI 2和Oauth 2时
- asp.net-web-api – 更好地总是在Web Api中返回H
- asp.net – 前端头还是后端?
- asp.net-mvc-3 – DropDownListFor Unobtrusive
- asp.net-mvc – Ninject.Extensions.Logging.nlo
- asp.net – asp:UpdateProgress – 保留换行符
- asp.net-web-api – mvc webapi cross domain po
热点阅读
