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

发布于 2024-10-19 21:28:04 字数 1739 浏览 6 评论 0原文

我想继承 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你爱我像她 2024-10-26 21:28:04

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

Web.config:

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

Code:

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
    }
}

Usage:

Membership.CreateUser(); // will call Membeship.Provider.Create(), i.e. - yours
℡寂寞咖啡 2024-10-26 21:28:04

您需要在自己的 Initialize 方法中初始化基类:

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

    base.Initialize(name, config);
}

You'd need to initialize the base class in your own Initialize method:

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

    base.Initialize(name, config);
}
半衬遮猫 2024-10-26 21:28:04

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文