Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ASP.NET SQL Membership Provider, how do I store my own per-user data?

I'm using the ASP.NET SQL Membership Provider. So, there's an aspnet_Users table that has details of each of my users. (Actually, the aspnet_Membership table seems to contain most of the actual data).

I now want to store some per-user information in my database, so I thought I'd just create a new table with a UserId (GUID) column and an FK relationship to aspnet_Users. However, I then discovered that I can't easily get access to the UserId since it's not exposed via the membership API. (I know I can access it via the ProviderUserKey, but it seems like the API is abstracting away the internal UserID in favor of the UserName, and I don't want to go too far against the grain).

So, I thought I should instead put a LoweredUserName column in my table, and create an FK relationship to aspnet_Users using that. Bzzzt. Wrong again, because while there is a unique index in aspnet_Users that includes the LoweredUserName, it also includes the ApplicationId - so in order to create my FK relationship, I'd need to have an ApplicationId column in my table too.

At first I thought: fine, I'm only dealing with a single application, so I'll just add such a column and give it a default value. Then I realised that the ApplicationId is a GUID, so it'd be a pain to do this. Not hard exactly, but until I roll out my DB I can't predict what the GUID is going to be.

I feel like I'm missing something, or going about things the wrong way. What am I supposed to do?

like image 461
Gary McGill Avatar asked Mar 27 '10 17:03

Gary McGill


People also ask

How can I use ASP.NET Membership provider?

The ASP.NET membership provider is a feature that enables ASP.NET developers to create Web sites that allow users to create unique user name and password combinations. With this facility, any user can establish an account with the site, and sign in for exclusive access to the site and its services.

How do I use SQLMembershipProvider?

To configure SQLMembershipProvider, first you need to configure your database. Leave the default selection "Configure SQL Server for application services" and click Next again. Enter the server name, Select SQL Server authentication and enter user id and password as in the following figure.

How can I use ASP.NET Membership in C#?

To create a user in our application by using ASP.NET Membership we need the following steps to complete this process. Step 1: Firstly, open visual studio, then go to File Menu and click New -> Web Site. Step 2: After open the new empty website and add a new item Login. aspx in Registration inside Solution Explorer.

What is membership class in asp net?

The Membership class is used in ASP.NET applications to validate user credentials and manage user settings such as passwords and email addresses. The Membership class can be used on its own, or in conjunction with the FormsAuthentication to create a complete system for authenticating users of a Web application or site.


4 Answers

I think you are looking for the ProfileProvider which let's you associate any arbitrary information you wish with the user.

ASP.NET Profile Properties Overview

ADDITION If the built-in ProfileProvider will not suit your needs, then you might consider implementing your own ProfileProvider by writing a class that derives from System.Web.Profile.ProfileProvider. This would enable you to write something that avoids the serialization issues you mentioned in your comment.

Implementing a Profile Provider

ADDITION Note about the SqlMembershipProvider. You are indeed correct that the Membership classes are really keyed on username even though the schema is keyed on UserId. This is frankly one of my pet peeves about the SqlMembershipProvider classes. This actually creates problem in a multi-application environment where you want a single user store but independent application role lists.

My recommendation would be to key on UserId since it is, as you mentioned, the primary key of the aspnet_Users table and is used in all the foreign key relationships and it is a single value. If you key on LowerUsername (and ApplicationId), and the username changes, you'll need to have cascade update enabled so that the change ripples to your custom tables.

like image 161
Thomas Avatar answered Oct 03 '22 11:10

Thomas


To do this, you can implement a profile provider. Its not very difficult. You will basically set up your user specific settings like this:

(web.config):

<profile enabled="true" defaultProvider="MyProvider">
    <providers>
        <add name="MyProvider" connectionStringName="MembershipCnnStr" applicationName="MyApp" type="System.Web.Profile.SqlProfileProvider"/>
    </providers>
    <properties>
        <add name="EmployeeId" type="String" />
        <group name="UserSettings">
            <add name="IsSandboxMode" type="Boolean" defaultValue="false" />
            <add name="Shortcuts" type="System.Collections.Generic.List`1[System.string]" />
        </group>
    </properties>
</profile>
like image 33
Gabriel McAdams Avatar answered Oct 01 '22 11:10

Gabriel McAdams


If you're looking at adding simple data tied to your users, such as added profile properties, you should look into Personalization.

Example, if you want to store a person's mother's maiden name as a part of their profile information you can do so using this feature.

It probably isn't the right choice for complex data, but it's a start.

like image 41
David Avatar answered Oct 04 '22 11:10

David


Write .cs file like

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        FirstNameTextBox.Text = Server.HtmlDecode(Profile.FirstName);
        LastNameTextBox.Text = Server.HtmlDecode(Profile.LastName);

    }

}

and web.config

 <profile>
  <providers>
      <add name="FirstName" type="System.String"/>
      <add name="LastName" type="System.String"/>
      <add name="MemberId" defaultValue="0" type="System.Int32"/>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
  </providers>
</profile>
like image 22
Pankil Agrawal Avatar answered Oct 01 '22 11:10

Pankil Agrawal