June 2007 - Posts
Creating an ASP.NET Membership Database for Community Server
With the addition of Morpheus in Community Server 2007, the ability to integrate CS and another ASP.NET site using the ASP.NET membership provider got a lot easier. A recent KB article details sharing the same login and password data for multiple installations of Community Server. The steps outlined there are:
- Install CS multiple times. One install will be membership master, others are children.
- Add a connection to the children's connectionStrings.config.
- Modify the children's web.config.
This is super simple and works great for multiple communities. To develop an ASP.NET web app and share the login with CS you need to take a few more steps.
The basic steps needed to share a membership database with CS and an ASP.NET application are:
- Install/deploy your application.
- Install CS 2007.
- Take a few stored procedures from CS and add to your membership database.
- Modify CS connectionStrings.config.
- Modify CS web.config.
Install Your Application
The key is the application must be configured to use ASP.NET Membership with the System.Web.Security.SqlMembershipProvider (or a custom provider that follows the provider model). Since I am just testing this out, I used "aspnet_regsql.exe -E -S localhost -A m" as documented to create my membership database. This gave me a new database named aspnetdb on my local instance of SQL Server. I then went on to create a web application with two pages - a default.aspx and a login.aspx. Default tells you whether you are logged in, and Login lets you login. The zipped project can be downloaded here.
Once that application is configured properly, take note of the connection string and the name attribute of the <authentication> <forms> element in web.config.
Install CS 2007
Nothing special here. A default installation will work just fine. You need to remember the application name (the default seems to be 'dev'), admin username, admin email, and admin password you use when installing the application.
Take Stored Procedures from CS 2007
This is where you do most of the work. We need to execute some CREATE scripts as well as hack together a new one (or copy some data).
Creating the Default Users
Before you can use Community Server with another application, you need to provide the default users that CS assumes are available. This can be done by modifying an existing stored procedure or by copying data. I modified an existing procedure - cs_system_CreateCommunity. I included something like lines 57-66, and 529-580 excluding 544-556 and 570-579. Anything that dealt with the aspnet_Applications, Membership, or Users tables was included, and everything else was left out. I then executed this procedure with application name and admin user information that was used when installing CS.
It might be much easier copy the data from another database or use the aspnet_* stored procedures on the membership database.
Copying the cs_Membership_* procedures
Five stored procedures must be copied from the CS database to the membership database - they are the cs_Membership_* procedures that are created during the CS install. I did this in SQL Management Studio by scripting the SPs as CREATE scripts and then executing them against my membership database.
Modify CS Install
Add a connection string to connectionStrings.config. Change the name attribute in the <authentication> <forms> element. Also change the connectionStringName in the <membership> <provider> attribute to match the values of the same elements from your application.
Conclusion
You can now create a user in your 'other' web application and when the visit the community pages of your site, they will automatically be logged in and able to interact just like they created their account on the CS install itself.
Note: I have not yet used this method on a production site, nor have I fully tested it. I cannot guarantee that it will work in all situations. I am relatively confident that it will work fine for any site as CS is using the membership database only for the username and password, and stores everything else in other tables.
More Posts