Search code examples
c#asp.netasp.net-membership

Is it possible to pass additional custom user data to custom ASP.NET Membership provider?


I'm implementing my custom Membership provider and I need to implement some additional fields (such as first name, last name, country, etc).

I know I can extend MembershipUser class and cast to it when I return aa user object from my provider. The thing which I can't find how to do and if it is possible to do is how to actually pass these custom properties to my provider when I'm creating a new user Membership.CreateUser(...) during user registration.

Is it possible at all? If yes, how it can be done?

Thanks

UPDATE

MSDN article says

However, this overload will not be called by the Membership class or controls that rely on the Membership class, such as the CreateUserWizard control. To call this method from an application, cast the MembershipProvider instance referenced by the Membership class as your custom membership provider type, and then call your CreateUseroverload directly.

Membership in this case is a reference to a instance of the class and at the same time it is a class by itself.

Neither

((CustomMembershipProvider)Membership).CreateUser(...);

nor

(CustomMembershipProvider)Membership.CreateUser(...);

works.

How should I cast it in this case?

UPDATE: See my answer.


Solution

  • I figured out how to do correct casting, so here it is:

    custom membership provider:

    public sealed class CustomMembershipProvider : MembershipProvider {...}
    

    custom membership user:

    public class CustomMembershipUser : MembershipUser {...}
    

    To create new custom user:

    CustomMembershipProvider myProvider = (CustomMembershipProvider)Membership.Provider;
    CustomMembershipUser user = (CustomMembershipUser)myProvider.CreateUser(...);
    

    To get custom user:

    CustomMembershipProvider myProvider = CustomMembershipProvider)Membership.Provider;
    CustomMembershipUser user = (CustomMembershipUser)myProvider.GetUser(...);
    

    And this article shows how to implement custom user: http://msdn.microsoft.com/en-us/library/ms366730.aspx