Burjua
Burjua

Reputation: 12706

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.

Upvotes: 3

Views: 1789

Answers (2)

Burjua
Burjua

Reputation: 12706

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

Upvotes: 3

Wouter de Kort
Wouter de Kort

Reputation: 39898

Instead of extending the MembershipUser you can add additional Profile data to the user.

For example the following the following can be added to your configuration file.

<profile>
    <properties>
        <add name="FirstName"/>
        <add name="LastName"/>
        <add name="Address"/>
        <add name="City"/>
        <add name="State"/>
        <add name="Zip"/>
    <properties>
<profile>

This will let ASP.NET create a ProfileCommon class and that will include all the properties you specify in the configuration file.

Here is the documentation for the Profile Properties.

Upvotes: 2

Related Questions