Storing additional data in aspnet_profile table with Membership data

7.9k views Asked by At

I am using Membership API with OpenId implementation in my project built on MVC2 framework. Other than the username , I need some other fields to be associated with the user at the time of registration.

I am not sure though but I think Profile system in asp.net is built for this type of requirement. Also, I see a table with other membership tables named 'aspnet_profile'.

I added following settings in the application web.config to enable the profile :

<profile enabled="true">
      <properties>
        <add  name="FullName" allowAnonymous="false"/>

      </properties>

    </profile>

As said before, application need some additional data to be associated with the user, so when creating a user using Membership API, I added few more lines of code for making entry into profile table

System.Web.Security.MembershipCreateStatus status = MembershipService.CreateUser(userModel.UserName, userModel.Password, userModel.UserName);

                   if (status == System.Web.Security.MembershipCreateStatus.Success)
                   {
                       FormsService.SignIn(userModel.UserName, true);
                       Session["Username"] = userModel.UserName;

                       dynamic profile = ProfileBase.Create(MembershipService.GetUser(userModel.UserName).UserName);
                       profile.FullName = userModel.UserFullName;
                       profile.Save();

                       RedirectToAction("Tech", "Home");



                   }

But I don't see any line added in the aspnet_profile table in the database. Also, I wanted to ask if this is the preferred way of adding additional data along with default membership data

3

There are 3 answers

0
Kunal On BEST ANSWER

I made it work by making some changes related to default profile provider name in the web.config:

<profile enabled="true" defaultProvider="AspNetSqlProfileProvider">
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" applicationName="/" connectionStringName="ApplicationServices" type="System.Web.Profile.SqlProfileProvider" />
      </providers>

      <properties>
        <add  name="FullName" allowAnonymous="false"/>

      </properties>

    </profile>

Also I added one more line between call to ProfileBase.Create function and setting the Profile.FullName;

profile.Initialize(userModel.userName, true);

I finally saw an entry in the aspnet_profile table for the newly registered user :)

1
Will Wu On

1, you need to create a profile class to define the profile structure

2, you need to config your profile settings in web.config as

3, now you can use your code now.

you just need to do the first 2 steps before you using it.

ref: http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx

3
VinayC On

While using ASP.NET profile provider, profile properties is the way to customize the user profile. However, you don't have to create and save profile instance by yourself - it is done by ASp.NET runtime it-self. Use either HttpContext.Current.Profile or use strongly typed dynamic Profile property from the page it self. By using later, you can write code such as

Profile.UserName = "User Name"; 

There is no need to call Save method. For more information see this article.

In a web application, one cannot really refer to dynamically created Profile class, so you have to work with HttpContext.Current.Profile (you can of course assign it to a dynamic variable for more readable code as done by you). Another way is to write your own class.