4

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?

john doe
  • 45
  • 2

3 Answers3

4

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));
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
  • Thanks! seems like I can only accept yr answer in 4 minutes. I will wait! – john doe Apr 27 '10 at 19:41
  • 1
    @John, i am not implying that this is not a correct answer, as it is, but it is not the only solution. The throttle on acceptance time is for your benefit. The longer you leave an answer open, the better chance you have of collecting quality responses. Upvotes are a good mechanism for indicating pleasure with an answer while waiting for other possibilities. Personally, I usually only check questions that have no accepted answer unless I am coming up short with interesting questions. Just saying that rushing acceptance is not expected and is generally against everyone's best interests... – Sky Sanders Apr 27 '10 at 20:19
  • I agree @Sky Sanders and apologies for any trouble! I will keep that in mind! – john doe Apr 27 '10 at 20:27
  • @john, nothing to apologize for, just lookin out for you. Welcome to StackOverflow. – Sky Sanders Apr 27 '10 at 21:15
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."

pelican_george
  • 961
  • 2
  • 13
  • 33
0

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.

Daniel Renshaw
  • 33,729
  • 8
  • 75
  • 94