How can I Install Membership, Roles and Profile on my Hosted Site?

This one (or variations of it) has come up frequently in the last week or so on ASP.NET newsgroup and forum posts, so I thought it would be appropriate to take another stab at providing some help.

The first thing we want to do is to enable our database for the providers. There are actually three separate ways you can do this:

1) Run ASPNET_REGSQL and follow the prompts. Of course, this requires that you have command-prompt access to the machine on which your site will be deployed. For hosted solutions this is obviously not available to us. "Where is 'ASPNET_REGSQL.EXE'", you say?

You know the old Chinese proverb about "teach a man to fish?" BTW, when you do find it, you'll of course know that as with most command - line utilities, you can type /? after the name of the executable at a command prompt and see a help listing of commands.

2) Run ASP.NET_REGSQL.EXE on your own sample database locally, and EXPORT all the sql script necessary to create the tables, views and stored procedures on a remote site. Again, this assumes you have the ability to access a remote hosted site's SQL Server online and execute a rather large SQL Script on it. If you take a look at the command line options for this, it has an export to file option.

3) Do it programmatically! Make a "Setup.aspx" page that uses the System.Web.Management utility method:

Management.SqlServices.Install("server", "USERNAME", "PASSWORD", "databasename", SqlFeatures.All)

System.Web.Management -- SqlFeatures.Install Method

Here is the signature:

public static void Install (
string server,
string user,
string password,
string database,
SqlFeatures features

Member name Description
All All features.
Membership The membership feature.
None No features.
Personalization The personalization feature.
Profile The profile feature.
RoleManager The role manager feature.
SqlWebEventProvider The Web event provider feature

This enumeration has a FlagsAttribute, which means you can select two or more features by combining them with the & (and) operator (the And operator in Visual Basic).

This does everything that ASPNET_REGSQL does. You'd think they would make it more obvious that you can do this, given the large number of sites that are hosted by commercial shared hosting companies, but no, they decided to push ASPNET_REGSQL as if everybody everywhere automatically has access to it. Go figure. There is a sample page "SetUpASPNetDatabase.aspx" in the sample solution in this article where you can simply fill in the above parameters in a form, and press a button to set up your database.

Have fun! It would be great if the documentation for basic "how to" or the ASP.NET Quickstart info would fill you in on this stuff. They don't.