继承SqlMembershipProvider并禁用CreateUser()和DeleteUser()

时间:2011-03-02 22:01:58

标签: c# .net asp.net asp.net-membership

我想继承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();
    }
}

感谢。

3 个答案:

答案 0 :(得分:1)

的Web.config:

<membership defaultProvider="Custom">
    <providers>
        <clear /> <!-- remove the built-in -->
        <add name="Custom" ... /> <!-- your own only -->
    </providers>
</membership>

代码:

public class UserMembershipSQL : SqlMembershipProvider
{
    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        // do your stuff, don't call base
    }

    public override bool DeleteUser(string username, bool deleteAllRelatedData)
    {
        // do your stuff, don't call base
    }
}

用法:

Membership.CreateUser(); // will call Membeship.Provider.Create(), i.e. - yours

答案 1 :(得分:0)

您需要使用自己的Initialize方法初始化基类:

public override void Initialize(string name, NameValueCollection config)
{
    [...] // your own initialization stuff

    base.Initialize(name, config);
}

答案 2 :(得分:0)

这可能是因为你没有在abatishchev提到的system.web \ membership配置节点的Providers部分中指定它。 从here下载ASP.Net Provider Model的源代码。继承自SqlMembershipProvider示例类,并查看SqlMembershipProvider内部如何工作。抛出NullReference异常,因为PasswordStrengthRegularExpression属性未初始化。包括您的提供商是唯一的解决方案,因为如果您不这样做,CreateUser方法仍将失败,因为您的自定义提供程序不在“已批准”提供程序列表中。当然,你可以通过在调用base.Initialize方法时指定一个批准的提供者名称,让我们说自定义成员资格提供者的构造函数,但我不认为这是正确的。此外,您将无法在配置文件中包含您的提供程序,因为您的提供程序将初始化两次,这将引发异常。再次,检查源代码。