Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I store additional user details using ASP.NET MVC and the SqlMembershipProvider?

So, I'm creating an ASP.NET MVC website.

It has a fairly complex user sign-up page with a lot of fields. My question is, where should I persist this? The user tables created by the membership-provider tool don't contain these columns and I'm left confused about what the best practice is in terms of storing this additional information about each user?

Is this where a profile provider comes into play? How do they work? If not, how else do you persist and store the additional user details not provided in the stock columns of the MembershipProvider tables?

Can anyone point me to some resources on how this is handled, how to access those additional user details when I need them, how to create a new user with all this information etc.

like image 846
KingNestor Avatar asked Jul 16 '09 19:07

KingNestor


People also ask

What part of an application is used to represent data stored in the DataBase in MVC?

Data representation is done by the view component. It actually generates UI or user interface for the user. So at web applications when you think of the view component just think the Html/CSS part.


1 Answers

This is where the ASP.NET Profile provider comes in. You can use it to store whatever profile information you want about a user. All you need to do is add the fields you need in the web.config file, MSDN Link on how to configure the profile fields in web.config file. To sum the article up, you just add the name and type values you want to store to the properties node of the profile element. Here's an example:

<profile enabled="true">
  <properties>
    <add name="Name" />
    <group name="Address">
      <add name="Street" />
      <add name="City" />
      <add name="Zip" type="System.Int32" />
    </group>
  </properties>
</profile>

In ASP.NET Webforms, Visual Studio automagically creates a strongly typed profile class that will reference your custom profile properties. In MVC, this doesn't happen. To refer to a user's profile information, just call HttpContext.Profile["PropertyName"]. An example:

HttpContext.Profile["Name"] = name;
HttpContext.Profile.GetProfileGroup("Address")["Zip"] = zip;

Edit: As Andy noted, using the default SqlProfileProvider isn't really good if you want to run queries on these properties. He is completely right, and perhaps I should have originally noted this limitation. This limitation exists because SqlProfileProvider stores all of the profile data is in three columns: PropertyNames and PropertyValuesString/PropertyValuesBinary. All of the keys are stored in the PropertyNames field, values that can be stored as a string are stored in the PropertyValuesString field, etc. This means it's extremely hard to do a query like "Select * from aspnet_Profile where Age > 10".

like image 101
Ryan Hoffman Avatar answered Nov 15 '22 19:11

Ryan Hoffman