Implementing Custom Membership Provider Membership User Issues

孤街醉人 提交于 2020-01-02 05:40:16

问题


I am trying to implement a custom membership provider and want to change the GetUser method. The problem is that GetUser returns MembershipUser and I want to return MyMembershipUser which has two additional properties FirstName and LastName. I can create a new method in my membership provider which returns MyMembershipUser but then I think it won't make any sense.

How would I go about doing this?


回答1:


That would defeat the purpose of the Membership classes. Do something like this if you need to access other properties:

var user = Membership.GetUser(userName, true) as MyMembershipUser;

Really you should have a separate Profile class that handles things that MembershipUser does not provide.

var profile = Profile.GetProfile(Membership.GetUser(userName, true));



回答2:


You should go for Profile Provider. check this link, you have either SqlStoredProcedureProfileProvider and SqlTableProfileProvider, this way you have ability to store Profile data “in the clear” in the database, letting you query the db whenever u want.

"you can implement whatever business logic you need in the stored procedures to map the Profile data to your own database schema and database logic."




回答3:


If MembershipUser is the base class of MyMembershipUser then you can return instances of MyMembershipUser even though the return type of GetUser() is MembershipUser and then, if necessary, cast them back to MyMembershipUser (but do you really need to do that?)

BTW, this is an example of polymorphism.



来源:https://stackoverflow.com/questions/2724376/implementing-custom-membership-provider-membership-user-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!