Introduction to Membership

One of the first things I was eager to dig into was the new MemberShip provider in ASP.NET 2.0.  Well I am still learning but here is a basic introduction.

First you will need to setup your site's database with the tables, proceedures, etc.  Nothing to fret though, the SDK comes with a wizard that will help you, aspnet_regsql.exe.  It is located in your $sysdrive$:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ directory, which of course is obvious to all of us.

Despite its obscure location, it is an easy to execute wizard to get us going.  First of course you will need to create your SQL database.  I have not looked into how to run the provider from anything other than SQL, since I build all sites with SQL behind them it is not an issue for me.

During the wizard process you simply provide it with your SQL login credentials and point it to your DB.  A few moments later your database is ready to go.

Next you will need to configure your web.config file to allow your application to execute the membership provider.  This involves setting up your connection string section with a string that will connect to your member provder database.  I am not going to cover connection strings in this entry, so I will just display an example.

  <add name='MyWebSite' connectionString='server=.;uid=dbUserName;pwd=dbPassword;database=MyWebSiteDB;'/>

Next you must add a very large section defining the way you want your membership provdeer to operate.  For example:

    <membership defaultProvider='TestWebMembershipProvider' userIsOnlineTimeWindow='15'>
        <remove name='AspNetSqlMembershipProvider' />
        <add name='TestWebMembershipProvider'
             type='System.Web.Security.SqlMembershipProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
             passwordStrengthRegularExpression='' />

    <roleManager defaultProvider='TestWebRoleProvider'
        <remove name='AspNetSqlRoleProvider' />
        <remove name='AspNetWindowsTokenRoleProvider' />
        <add connectionStringName='TestWeb'
          type='System.Web.Security.SqlRoleProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

    <profile defaultProvider='TestWebProfileProvider'>
        <remove name='AspNetSqlProfileProvider' />
        <add name='TestWebProfileProvider'
          type='System.Web.Profile.SqlProfileProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' />

Wow that is a lot of stuff!  I am not going to cover it in this article, we will examine this mess in future entries, you can't have it all at once.

Now you have two options to leverage the membership provider, you can used the canned controls for login and registration that come with ASP.NET 2.0 or you can mannually do everything for the UI.  Personally, since I already have a few hundred sites with logins built into them, I am going the mannual route, so I will cover that first.  Besides that is the way we all learned to divide right? 

To keep this entry from getting way too long I will abreviate and expand in future entries.  Make sure your page or your entire site has a refence to System.Web.Security.  Next you can reference all the core authentication and user Management methods from the Membership class.  For now just focus on the ValidateUser, CreateUser, FindUsersByEmail, FindUsersByName, GetUser and UpdateUser.  Long story short, there is a whole namespace of classes that help us manage users and roles.  I will be covering those shortly.

Share This Article With Your Friends!