Pla*_*Kid 19 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>
Run Code Online (Sandbox Code Playgroud)
这是我的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.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到没有调用Initialize方法,我在这里阅读了问题并且人们说我不必手动调用,如果我正确连接了我的web.config,我不需要做什么,但我确实试图手动调用,但它在我试图转换NameValueCollection时给了我一个InvalidCastException.
谁能帮我?谢谢
Lor*_*its 46
要调用Initialize(),您需要以某种方式实例化自定义成员资格提供程序.像这样:
MyCustomMembershipProvider myProvider = (MyCustomMembershipProvider)Membership.Providers["NameOfMembershipProviderInConfig"];
Run Code Online (Sandbox Code Playgroud)
现在,当您使用myProvider时,将调用自定义提供程序中的Initialize().
确实,只要您的提供程序配置正确(如代码示例中所示),您的 Initialize 方法就应该自动调用。
您需要澄清如何“手动调用它”,以及您尝试在何处强制转换 NameValueCollection。是在Initialize内部发生的吗?
也许您应该向我们展示您的 Initialize 方法(您没有忘记关键字override,是吗?;-)
编辑:嗯,初始化方法似乎也不错。
请记住:Membership是一个静态类,它以惰性方式加载和初始化配置的提供程序。因此,在调用或属性Initialize之前,不会发生提供程序的构造以及对其方法的调用。大多数其他静态方法(例如)都会执行此操作,但结论是在实际使用 Membership API 之前不会调用您的 Initialize 方法。Membership.ProviderMembership.ProvidersGetUser()
您是否明确地或通过使用登录控件或类似的方式完成了此操作?
| 归档时间: |
|
| 查看次数: |
18798 次 |
| 最近记录: |