This tutorial will show you how to make the ASP.NET forms authentication work with SQL Server using the provided ASPNETDB.MDF file.  Then it will show you how to properly configure your web.config file to use it.

*Note – This tutorial will help if you are having trouble getting your forms based authentication to work in a shared hosting environment.

Visual Studio provides an excellent way to manage user authentication. ASP.NET forms authentication will require the user to enter a username and password. ASP.NET creates all of this for you.  You can either do this with the login control, or choose the premade template when creating your site which includes the login control.  It also has the redirect already set up (redirects user to login screen if not logged in or trying to access a restricted area).  It stores the information in an ASPNETDB.MDF database file.  The ASPNETDB.MDF file contains both the schema and data. You can create roles and access rules using this provided ASPNET.MDF file. You will use the, “ASP.NET Configuration” to make the access rules and roles.

Some of what the asp.net forms authentication can do.

  • Creating new users and passwords.
  • Store membership info
  • Managing passwords, which includes creating, changing, and resetting.

The ASPNETDB.MDF file is added to your project and is in the App_Data folder.

*Note – You may need to run “ASP.NET Configuration” and refresh before the file will show up. You may also need to create a role and/or username. Roles are created when “ASP.NET Configuration” is run. You can register / creates a user account by running Register.ASPX in Visual Studio. It’s under the “Account” folder. 

Some people have problems when trying to upload ASPNETDB.MDF file to their server and getting it to work properly, especially with shared hosting.  Since the schema and data are so useful in the ASPNETDB.MDF file we’ll move it over to our SQL Server database.

 

Start by choosing the ASP.NET Web Site template in Visual Studio.

asp-net-login-template
asp-net-login-template

The ASPNET.MDF File

*To see the ASPNETDB.MDF file in your solution explorer, database explorer, or server explorer you may have to first run, “ASP.NET Configuration.” To do this, click ASP.NET Configuration in your Visual Studio menu under Websites > ASP.NET Configuration.  Then refresh.

visual-studio-login-aspnetdb-mdf
visual-studio-login-aspnetdb-mdf

After running ASP.NET Configuration in Visual Studio in this screen will show in the browser. This is the ASP.net Web Site Administration Tool and is where Roles and Access Rules are managed.

asp-net-web-site-administration-tool
asp-net-web-site-administration-tool

Next right click the ASPNETDB.MDF file in your database explorer, server explorer, Solution Explorer. Select the option “Publish to Provider.” This creates a SQL script we can use in our SQL Server Management studio to recreate our database.

*Note – This is done differently depending on the version of Visual Studio used.  Below I will show a couple of ways.

 

ASPNET.MDF in Visual Studio Express

Below is via “Database Explorer” in an express version of Visual Studio.

visual-studio-login-aspnetdb-mdf-publish-provider
visual-studio-login-aspnetdb-mdf-publish-provider

ASPNET.MDF is Visual Studio

In a full version of Visual Studio,  go under the “App_Data” folder, right click on the ASPNETDB.MDF file and click “Include In Project.”

asp-net-login-include-aspnetmdf-in-project

The icon for the ASPNETDB.MDF should turn a yellow/golden color. Double click the file.  That should open the “Server Explorer,” where you can then right click the ASPNET.MDF file that is in the “Server Explorer,” select the option “Publish to Provider.”

asp-net-login-publish-to-provider-visual-studio

Database Publishing Wizard

After you click “Publish to Provider,” a new window will open, “Welcome to the Database Publishing Wizard.”

asp-net-visual-studio-database-publishing-wizard
asp-net-visual-studio-database-publishing-wizard

ASPNET.MDF in Database Publishing Wizard

Select the ASPNET.MDF file and click next. Make sure, “Script all objects in the selected database” is checked.

database-publishing-aspnetdb-mdf
database-publishing-aspnetdb-mdf

Now select the destination of the output for the script that will be created, and click next. Make note of where you are saving your file.

database-publishing-aspnetdb-mdf-sql-output-location
database-publishing-aspnetdb-mdf-sql-output-location

Select publishing options… Click next.

aspnetdb-mdf-options-settings
aspnetdb-mdf-options-settings

Now review and click, Finish.

aspnetdb-mdf-database-sql-script-review
aspnetdb-mdf-database-sql-script-review

You should see a screen like below. Be patient, it may take a few minutes to complete. Once finished, click close.

database-publishing-aspnetdb-mdf-progress-finished
database-publishing-aspnetdb-mdf-progress-finished

Using SQL Server Management Studio to run the ASPNET.sql Script

Now open SQL Server management Studio. Create a new blank database…..

sql-server-management-studio-create-new-database
sql-server-management-studio-create-new-database

 

New Database:

New-Database
New-Database

 

Double click on the sql (ASPNET.MDF.sql)  file you just created to open it in SQL Server Management Studio. *It may look like ASPNET.MDF. The .sql is  a hidden file extension. Make sure the new database you just created is selected. Execute the aspnetdb.mdf sql file you just loaded (the tab will need to be selected in SQL Server Management Studio). You are executing this script so that it will create the necessary tables, relationships, etc., into your new SQL Server database.  You can now use this for your asp.net login.

sql-server-management-studio-script-execute
sql-server-management-studio-script-execute

 

After  execution it will look like below. You should be able to double click on the database you just created on the left and expand. Refresh. Expand the tables folder to see all of the tables the script just created.

sql-server-management-studio-script-post-execution
sql-server-management-studio-script-post-execution

Web.config Connection String

Almost There! Now that you have your DB setup, all that is left is to configure your web.config file to use the SQL Server Database you just created.

 

The asp.net login is still setup to use the default connection string of our original aspnet.mdf file.  We need to change the connection string to the SQL Server Database we just made. We have 3 separate areas to update, <connectionStrings>, <membership>, and <roleManager>. The connection string needs to be updated to connect to the new database that was just made. Then there are <providers> tags. One is under<membership> and the other under <roleManager>. Update “connectionStringName” here to the same name as entered in <connectionStrings>.

For the connectionString, the “Data Source” should be the same as what is listed in the “Object Explorer” in SQL Server Management Studio. ID, and Password should be what was used to connect to the Database in  SQL Server Management Studio.

 

See image below.  The old connection information is commented out with the new information put in.

webconfig-connection-string-aspnet-login-sql-server
webconfig-connection-string-aspnet-login-sql-server

Here is sample code for the Web.config:

 

Use Visual Studio to run the Login.aspx file located in the “Account” folder. You should see a login screen in your browser.

 

 

 

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">