This is something that I really overlooked with all the ASP.NET stuff I’ve done. I was messing around with a ASPNET 2.0 project and discovered this great mechanism that saved me from writing tons of code. Basically you setup a few tables on your SQL database, edit your web config with a connection string to that database and you’re done. Now you have a project with the ability to create users and store their profiles and you still haven’t written a line of code. I’m kind of suprised I blew this off for as long as I did.
Here are the technical details for anyone looking to implement this on a site. I looked around and pieced it together so this is all you need to do.
First run “aspnet_regsql.exe -W” to setup your database schema. (I added the tables to an existing database rather than seperating it)
Next, make changes to your web.config to point at the database.
Add the connection string (connectionstrings section):
name="LocalSqlServer" connectionString="User ID=USER;Password=PASSWORD;server=HOST;database=DB_NAME"
Point the providers to the connection string (system.web section):
Membership provider:
-
<profile>
-
<providers>
-
<remove name=”AspNetSqlProfileProvider”/>
-
<add name=”AspNetSqlProfileProvider” type=”System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” connectionStringName=”LocalSqlServer” applicationName=”APP_NAME”/>
-
</providers>
-
<properties>
-
<add name=”Property1″/>
-
<add name=”Property2″/>
-
</properties>
-
</profile>
The membership provider lets you specify options about password and lockout security. The options are pretty self explanatory. Another cool thing I found was you can add the properties and you get intellisense on these fields when you are storing profile information.
Finally, write some code! You’re all done setting this up.
Adding a user is as simple as:
Membership.CreateUser
Combine this with the Create User Wizard and you’re creating users on a site in minutes.
When you want to store user profile information just use this:
Profile.Property1 = "foo"
Note: If you need to pull this data from a procedure or anything behind the scenes in your database beware that the profile provider stores all profile information for a user in a single row. It stores everything in a text field with delimiters to tell where the next profile property starts. You might want to edit a procedure and put the stuff into seperate rows or whatever your application requires.
These providers all integrate with Login suite of controls in ASP.NET. So far I’ve used the Login Name and Login Status controls. It’s been pretty drag and drop so far. If I have time I’ll try to post some tips about the other controls I use.
I know in the past I’ve had to create complex user creation and security mechanisms on sites. I feel like I wasted a lot of time now!
Well, this won’t work for all sites but I’m sure with a little customization it can cover a large portion of sites. I hope this helps someone out who has any questions with this. If you have any specific questions or comments please let me know.
Enjoy!
