且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC自定义成员资格提供程序 - 如何重载CREATEUSER?

更新时间:2022-12-04 15:03:55

这不是很明显吗?你引用的成员,当你调用Membership.CreateUser不MyMembershipProvider。

Isn't it obvious? You're referencing Membership, not MyMembershipProvider when you call Membership.CreateUser.

看起来成员是一个静态类,它内部调用到你的会员资格供应商,所以比自己创建MyMembership静态类,做同样的事情来改变这种没有实际的办法。既然你不能从静态类继承,你就必须要么调用到Membership.XXX从静态版本,或实现自己的功能。

It looks like Membership is a static class that internally calls into your membership provider, so there is no real way to change this other than creating your own "MyMembership" static class that does the same thing. Since you can't inherit from static classes, you would have to either call into Membership.XXX from your static version, or implement the functionality yourself.

您自己MyMembership类可以再有你希望它有任何方法。

Your own MyMembership class could then have any methods you wanted it to have.

另一个选择是做这样的事情:

Another option would be to do something like this:

((MyMembershipProvider)Membership.Provider).CreateUser()

我,因为它需要你记住调用提供商CREATEUSER而非Membership.CreateUser不喜欢,虽然这种做法。另外,铸造往往是code气味(除非封装的,这就是为什么我建议你自己MyMembership静态类)。

I don't like this approach though because it requires you to remember to call the provider CreateUser rather than the Membership.CreateUser. Plus, casting is often a code smell (unless encapsulated, which is why i recommended your own MyMembership static class).