Misfit Geek

Fustrated by Design !

MaximumASP

ScottGu forwarded me an email from a developer this weekend who wanted to use ASP.NET Membership in an application deployed on a shared hosting account that allows only one SQL Server database.

It’s not all that difficult to add ASP.NET membership (as well as other ASP.NET services) to your existing database.

ASP.NET doesn’t really care where the information repository for it’s built in services live as long as they are complete.

The database that contains the ASP.NET Application Services repository is resolved via a standard connection string in the application’s web.config file.

   1:     <connectionStrings>
   2:      <add name="ApplicationServices"
   3:           connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnetdb.mdf;
User Instance=true"
   4:           providerName="System.Data.SqlClient" />
   5:    </connectionStrings>

In the entry above the connection points to the a default database of aspnetdb.mdf in the local application’s App_Data directory.

Note that the default is determined by settings in the machine.config file. If you have run the aspnet_regsql.exe utility on your developer machine to configure a SQL Server database to house the membership / ASP.NET Application Services tables than your machine.config defaults probably point to a SQL Server instance instead of a SQL Express database.

In any event, you can use a connection sting like the one above and simply change the .mdf file name to the one that you need to use in your App_Data folder.

If you are doing this on your shared host that you would specify the connection string to your hosting provider’s database and they would have provided you with the necessary connection information when you signed up for the account.

Once our application can connect to the database we’ve chosen to house out membership information we need to configure our database for use by the Membership Provider.

The ASP.NET installation provides some .SQL script files that we can use to make this process easier.

In Windows Explorer, navigate to your .NET Framework install directory.

On my development machine it was located here.

C:\Windows\Microsoft.NET\Framework\v4.0.30319

Note the highlighted .SQL files in the figure below

Though all may be useful to you in setting up the various ASP.NET Application Services, for the purposes of this post, we’re only going to set up the ASP.NET Membership service.

We will need the first two .sql files…..

InstallCommon.sql

InstallMembership.sql

Make separate copies of these as we’ll need to edit them before we run them.

Next you need to determine how you will execute T-SQL against your SQL Server instance.

If you are working on your own machine you can simply run SQL Server Management Studio on your machine (or against your SQL Server instance.)

If you’re host does not support direct connections with SQL Server Management Studio then they will likely have provided some web based administration mechanism and you will need to use that to execute the T-SQL.

In my sample application I created an empty SQL Express database named AddedMembershipDemo.mdf

AddedMembershipDemoMDF

First open the copy of InstallCommon.sql  that you made.

You will have to make a couple of edits to the code. You will note several references to the database name “”aspnetdb”……

Here are a few examples:

SET @dbname = N’aspnetdb’

USE [aspnetdb]
GO

You must change all references to “aspnetdb” to the name of the database that you are adding the ASP.NET Application Services to.

Since the database already exists you will want to comment out the code bock that looks like this:

   1: IF (NOT EXISTS (SELECT name

   2:                 FROM master.dbo.sysdatabases

   3:                 WHERE name = @dbname))

   4: BEGIN

   5:   PRINT 'Creating the ' + @dbname + ' database...'

   6:   DECLARE @cmd nvarchar(500)

   7:   SET @cmd = 'CREATE DATABASE [' + @dbname + '] ' + @dboptions

   8:   EXEC(@cmd)

   9: END

  10: GO

Once you have made those changes you should be able to run the resulting script successfully (though you may get a warning about some permission assignments.)

Next open InstallMembership.sql and change the references to aspnetdb to whatever your new database name is and run that script as well.

On success you’re database will contain Tables, Views, Stored Procedures, etc like this.

AddedMembershipDemoMDFTables

If you started your application by creating from the default ASP.NET application template, your web.config file should already have the configuration section for ASP.NET Membership. If not you will need to add it manually as below.

   1: <membership>

   2:   <providers>

   3:     <clear/>

   4:     <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"

   5:          enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"

   6:          maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"

   7:          applicationName="/" />

   8:   </providers>

   9: </membership>

Now you should be able to open your site and start adding users to the membership repository.

Similar Posts:

    None Found

Comments

There are 18 comments for this post.

  1. Eric Schoenholzer on July 12, 2010 8:10 pm

    And why not using aspnet_regsql.exe?

    Has the same features (using for ex. a sql connection string) without changing any sql files.

  2. Twitter Trackbacks for Adding ASP.NET Membership to your OWN Database. : Misfit Geek [misfitgeek.com] on Topsy.com on July 12, 2010 8:26 pm

    RE: Adding ASP.NET Membership to your OWN Database.

    Pingback from Twitter Trackbacks for Adding ASP.NET Membership to your OWN Database. : Misfit Geek [misfitgeek.com] on Topsy.com

  3. Mark Wisecarver on July 12, 2010 8:28 pm

    Microsoft recommends against using SQL Express in a Shared ASP.NET Hosting environment.

    This is something I have to help developers with over and over again and point them to the notes by Microsoft on why SQL Express should not be used. Just saying bro.

    For what it’s worth I’ve created a Webcast on doing this in Shared Hosting from the local command prompt.

  4. Joe Stagner on July 12, 2010 9:46 pm

    Eric,

    You can – but it’s not always possible in a shared hosting environment,

    Mark, nobody said deploy SQL Express. It should NOT be used in a shared hosting environment. But createing the service repository is the same no matter what edition of SQL Server.

    Non-Issue!

  5. Jalpesh Vadgama on July 13, 2010 4:10 am

    HI Joe,

    You can still use aspnet_regsql.exe in shared hosting environment. Or you can do one thing create a duplicated database in local and compare the live and shared database with others and you can enable all the things in your database.

    Best Regards,

    Jalpesh

  6. Adding ASP.NET Membership to your OWN Database - Joe Stagner on July 13, 2010 6:41 am

    RE: Adding ASP.NET Membership to your OWN Database.

    Thank you for submitting this cool story – Trackback from DotNetShoutout

  7. JR Kincaid on July 13, 2010 11:38 am

    @Eric Schoenholzer :

    I actually glad we have access to the SQL script and are not limited to the tool.

    For a professional project you maybe limited to doing DDL scripts and may not be able to run the exe utility.

    Plus as a developer I like having full controls and my DBAs are probably happier too.

  8. Adding ASP.NET Membership to your OWN Database. : Misfit Geek on July 14, 2010 1:58 pm

    RE: Adding ASP.NET Membership to your OWN Database.

    ScottGu forwarded me an email from a developer this weekend who wanted to use ASP.NET Membership in an application deployed on a shared hosting account that allows only one SQL Server database. It&rsquo;s not all that difficult to add ASP.NET membership

  9. Ulhas Surwade on July 21, 2010 1:17 am

    Hi,

    If client already have his own login system and he is using my application(software as a service) then how my clients users will be able to use my application? should I import all my clients users login and password into my system?(I think for security purpose this is not good solution) or all those users should again register into my system? Please suggest if there is any other way to do this.

    Thank You.

    Ulhas

  10. Jesse Johnson on August 10, 2010 9:36 pm

    I really wish the default membership allowed for custom schema naming. I hate having to stick to the normal aspnet_ naming schema for membersip :/

  11. JoeStagner on August 10, 2010 9:40 pm

    You can always tweek it to your liking :)

  12. Martin Prince on August 11, 2010 1:58 am

    So there’s no way to just copy the db structure (and data, as I already created some users) that was automatically created when I switced to forms authentication from aspnetdb.mdf to my SQLexpress db and then modify the web.config to point to my db instead?

  13. JoeStagner on August 11, 2010 2:20 am

    You can but your own db must support all the structure needed by ASP.NET Membership.

  14. Daniel Rivers on August 11, 2010 11:12 am

    Question:
    I typically do my dev work with sql express… I then use the DB Publishing Wizard… I script the DB to my desktop then move that into my production DB… I have never had any issues. Is there a reason I should not do it this way?

    Thanks!

  15. Martin Prince on August 11, 2010 7:27 pm

    I’m using .net framework 3.51 SP1. There is no aspnet_regsql.exe under the 3.5 directory. Can I use the one under the 2.0 directory?

  16. JoeStagner on August 11, 2010 8:11 pm

    Yes you can

  17. Martin Prince on August 11, 2010 8:49 pm

    OK, thanks Joe. Just did it…now to modify the web.config to use and I should be golden. BTW, have been going through your videos…very helpful, please keep them coming.

  18. JoeStagner on August 12, 2010 4:10 am

    Cool – let me know if you get stuck.

Write a Comment

Let me know what you think?