我有一个在应用服务层实现的自定义SimpleMembershipProvider
。提供程序具有一些自己的属性以及从基类继承的属性。尽管在提供程序上调用了重写的Initialize
方法,并且它在没有任何异常的情况下执行,但是继承的属性会获得不正确的默认值,例如零或Int32.MaxValue
。我需要将它们初始化为配置中提供的值。我怎样才能做到这一点?
配置如下所示:
<membership defaultProvider="AbcMembershipProvider">
<providers>
<clear/>
<add name="AbcMembershipProvider"
type="Abc.Services.AbcMembershipServices, Abc.Services"
connectionStringName="Membership"
applicationName="ABC"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="3"
passwordAttemptWindow="10"
passwordValidityPeriod="30" />
</providers>
</membership>
提供程序会覆盖Initialize
方法,如下所示:
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
if (config.ContainsKey("passwordValidityPeriod"))
{
int.TryParse(config["passwordValidityPeriod"], out _passwordValidityPeriod);
config.Remove("passwordValidityPeriod");
}
base.Initialize(name, config);
}
在服务启动期间,SimpleMembershipProvider
初始化如下:
if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection("Membership", "SystemUser", "UserId", "UserName", false);
在Initialize
调用
WebSecurity.InitializeDatabaseConnection()
方法
答案 0 :(得分:0)
查看源代码,发生这种情况的原因是,在初始化Provider时,MembershipProvider
为各种SimpleMembershipProvider
属性提供了默认值。例如:
// Inherited from MembershipProvider ==> Forwarded to previous provider if this provider hasn't been initialized
public override int MaxInvalidPasswordAttempts
{
get { return InitializeCalled ? Int32.MaxValue : PreviousProvider.MaxInvalidPasswordAttempts; }
}
绕过它的方法是在自定义提供程序中覆盖这些属性。