继承SqlMembershipProvider并禁用CreateUser()和DeleteUser()
我想继承 SqlMembershipProvider,设置成员资格设置以使用派生的自定义提供程序,并禁止某人调用 Membership.CreateUser()
和 Membership.DeleteUser()
。
我有一个外部应用程序,它将提供用户添加/删除机制,其功能比内置会员资格的功能更多。
当调用 Membership.CreateUser() 或 Membership.DeleteUser() 时,我能够重写 CreateUser() 和 DeleteUser() 以抛出 NotSupportedExceptions
。
然后,我尝试了 2 个自定义方法,并分别调用 base.CreateUser()
和 base.DeleteUser()
但出现 null 异常。我认为问题是基本方法只能由重写的函数访问,而不能由自定义函数访问。
我的代码如下:
public class UserMembershipSQL : SqlMembershipProvider
{
internal MembershipUser CreateUserCustom(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
//do stuff here -- not accessible by Membership.CreateUser
//currently throws a null exception even though all parameters are set properly
return base.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
}
internal bool DeleteUserCustom(string username, bool deleteAllRelatedData)
{
//do stuff here -- not accessible by Membership.DeleteUser
return base.DeleteUser(username, deleteAllRelatedData);
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
throw new NotSupportedException();
}
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
throw new NotSupportedException();
}
}
谢谢。
I want to inherit SqlMembershipProvider, setup the membership settings to use the derived custom provider, and prohibit someone from calling Membership.CreateUser()
and Membership.DeleteUser()
.
I have an external application that will offer a user add/delete mechanism that does more than what built-in Membership does.
I was able to override the CreateUser() and DeleteUser() to throw NotSupportedExceptions
when Membership.CreateUser() or Membership.DeleteUser() is called.
I then tried 2 custom methods and had each invoke base.CreateUser()
and base.DeleteUser()
but got null exceptions. I believe the issue is the base methods are only accessible by the overridden functions and not the custom ones.
My code is below:
public class UserMembershipSQL : SqlMembershipProvider
{
internal MembershipUser CreateUserCustom(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
//do stuff here -- not accessible by Membership.CreateUser
//currently throws a null exception even though all parameters are set properly
return base.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
}
internal bool DeleteUserCustom(string username, bool deleteAllRelatedData)
{
//do stuff here -- not accessible by Membership.DeleteUser
return base.DeleteUser(username, deleteAllRelatedData);
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
throw new NotSupportedException();
}
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
throw new NotSupportedException();
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Web.config:
代码:
用法:
Web.config:
Code:
Usage:
您需要在自己的
Initialize
方法中初始化基类:You'd need to initialize the base class in your own
Initialize
method:这可能是因为您没有在 system.web\membership 配置节点的 Providers 部分中指定它,如 abatishchev。
从此处下载 ASP.Net 提供程序模型的源代码。继承 SqlMembershipProvider 示例类并查看 SqlMembershipProvider 内部是如何工作的。由于PasswordStrengthRegularExpression 属性未初始化,因此引发NullReference 异常。包含您的提供程序是唯一的解决方案,因为如果您不这样做,CreateUser 方法仍然会失败,因为您的自定义提供程序不在“批准”提供程序列表中。当然,您可以通过在调用自定义成员资格提供程序的构造函数中的 base.Initialize 方法时指定已批准的提供程序名称来绕过此问题,但我认为这是不正确的。此外,您将无法将您的提供程序包含在配置文件中,因为您的提供程序将被初始化两次,这将引发异常。再次检查源代码。
That's probably because you didn't specify it in the Providers section of the system.web\membership config node, as mentioned by abatishchev.
Download the source code of ASP.Net Provider Model from here. Inherit from SqlMembershipProvider sample class and see how the SqlMembershipProvider internals work. The NullReference exception is thrown because the PasswordStrengthRegularExpression property is not initiallized. Including your provider is the only solution because if you don't do that, the CreateUser method will still fail because your custom provider is not in the list of 'approved' providers. Of course, you can bypass this by specifying an approved provider name when invoking the base.Initialize method in let's say the constructor of your custom membership provider, but I don't think this is correct. Besides, you won't be able to include your provider in the config file because your provider will be initialized twice, which will throw an exception. Again, check the source code.