Click here to Skip to main content
15,867,141 members
Articles / Web Development / XHTML

Fully configured ASP.NET Membership Website Template

Rate me:
Please Sign up or sign in to vote.
4.89/5 (18 votes)
7 Jul 2009CPOL4 min read 164.4K   8.3K   119   25
A pre-configured ASP.NET website containing a Master page, error handling, login / logout, and other boilerplate new project code.

Introduction 

The attached Visual Studio template is used to instantly create a website project that contains all the boiler plate code usually tedious to development (login/logout, displaying messages, Master page, CSS, JavaScript, error handling, changing passwords, etc...). This article provides a fully configured starting point for website project development with all that boiler plate code already completed.

Background

The starting point in an application usually takes the longest getting the application configured properly to display messages in a uniform way, or to get the login features working. In order to speed up this portion of the application development process, I built this application template for use with Visual Studio 2008 to provide a stepping stone for all further website development.

If you don't know about Visual Studio project and item templates, surf over to here for an overview before continuing on. If you don't know about Base Pages and their uses, please surf over to here for an overview before continuing on. If you don't know about Master Pages and their uses, please surf over to here for an overview before continuing on.

Using the code

Place the attached .zip file into your Project Templates folder associated with Visual Studio 2008. The default location is {drive}:\...\Documents\Visual Studio 2008\Templates\ProjectTemplates. If you don't know where yours is, then open Visual Studio -> Tools -> Options -> Projects and Solutions, and look at the "user project templates location" textbox to see where Visual Studio is looking for custom project templates.

After placing the .zip file in your Project Templates folder, open Visual Studio 2008 and click File-> New Website. Name your website and click OK. A new website project will be in your solution, and it will contain the following items different than a normal website project:

  • Admin/{Default.aspx, web.config}
  • App_Code/{BasePage.cs, BaseMasterPage.cs}
  • App_Data/Web.sitemap
  • DefaultMaster.master
  • Global.asax
  • Login.aspx
  • Logout.aspx
  • web.config
  • Stylesheets/stylesheet.css
  • Scripts/JScript.js
  • Images/{Warning.gif, Error.gif, Information.gif}

Plus the normal Default.aspx but it is different in that it references the DefaultMaster.master as its Master page.

Go to the web.config file and change the connection string to point to a valid database that contains the ASP.NET membership tables. Confirm that the applicationName="" attribute that the membership and role sections of the web.config file points to are named correctly.

ASP.NET
<roleManager enabled="true">
  <providers>

    <clear />
    <add name="AspNetSqlRoleProvider" 
         type="System.Web.Security.SqlRoleProvider, System.Web, 
               Version=2.0.0.0, Culture=neutral, 
               PublicKeyToken=b03f5f7f11d50a3a"         
         connectionStringName="appNameConnectionString" 
         applicationName="appName" />

  </providers>
</roleManager>
<membership>
  <providers>
    <clear />
      <add name="AspNetSqlMembershipProvider"

           type="System.Web.Security.SqlMembershipProvider, System.Web, 
                 Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           connectionStringName="appNameConnectionString"
           ...
           applicationName="appName" .../>
      </providers>

    </membership>

Add your header image to the Images folder. On the DefaultMaster.master page, add the ImageUrl="" attribute to the imgHeader image control to point to your header image.

Ensure the Regular Expression for passwords is correct in the web.config. The current expression requires a password between 8 and 15 characters, and requires the use of:

  1. a number (?=.*\d)
  2. a lower case letter (?=.*[a-z])
  3. an uppercase letter (?=.*[A-Z])
  4. and a special symbol (?=.*[!@#$%^&amp;*()_+])
ASP.NET
<membership>

  <providers>
    <add ... passwordStrengthRegularExpression="" />
    <appSettings>
    ...
    <add key="PasswordRegEx" 
         value="(?=^.{8,15}$)
                (?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&amp;*()_+])
                (?!.*\s).*$"/>

  </appSettings>

Develop some way to log errors that occur in the application, and place that code in the Global.asax Application_Error method and the BasePage.HandleException(Exception ex) method:

C#
public void HandleException(Exception ex)
{
    if (ConfigurationManager.AppSettings["Environment"] == "Development")
    {
        Master.ErrorMessage = ex.Message;
    }
    else
    {
        Master.ErrorMessage = "An unexpected error has occured in" + 
           "the application. If it continues, please contact the administrator.";
    }
    // TODO Log the exception somehow, either with
    // Microsoft.Practices.EnterpriseLibrary.Logging or some other mechanism
}

void Application_Error(object sender, EventArgs e) 
{
    // Code that runs when an unhandled error occurs

    //get reference to the source of the exception chain
    Exception lastError = Server.GetLastError();
    Exception ex;
    if (lastError.InnerException != null)
        ex = lastError.InnerException;
    else
        ex = lastError.GetBaseException();
    // TODO: Log the error somehow.
}

User Administration

Inside the Admin/Default.aspx page, there is a basic user administration piece that would allow someone with the Admin role to Approve/Unapprove, Unlock, and Add/Remove roles to users in the system.

Points of interest

Message are passed to the user via code-behind calls to:

  • base.InformationMessage = "My Information Message";
  • base.ErrorMessage = "My Error Message";
  • base.WarningMessage = "My Warning Message";

Although the "base." syntax is not necessary, I always place it so any future developer knows that some base page contains the definition for these properties and not the current page.

These message sections are initially invisible, and become visible when a string message passed to them (see BaseMasterPage.ErrorMessage for more details) sets EnableViewState = false, so they will disappear when the application does a postback.

The Master page utilizes the CodeFileBaseClass="BaseMasterPage" so the BaseMasterPage.cs file could expose the Labels and divs containing them via properties so a page could reference them.

ASP.NET
<%@ Master Language="C#" AutoEventWireup="true" 
    Inherits="DefaultMaster" CodeFile="~/DefaultMaster.Master.cs"
    CodeFileBaseClass="BaseMasterPage" %>

The BasePage class hides the Page.Master property and casts the MasterPage as a BaseMasterPage so all a page has to reference is its ErrorMessage property.

C#
public new BaseMasterPage Master { get { return (BaseMasterPage)base.Master; } }

public string ErrorMessage
{
  set { Master.ErrorMessage = value; }
}

Adding Just ASP.NET Membership and Roles Tables to a Database

Adding just the ASP.NET Membership tables to the database is easy to do, but requires you to use the command line of the aspnet_regsql.exe file. Look here for more information on using just the features you want for the ASP.NET membership.

History   

  • June 26th, 2009 - Initial post.
  • July 7th, 2009 - Added User Admin section

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Harland Financial Solutions
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVisual Studo 2010 - Error Pin
mario.a.nunes12-Aug-13 3:36
mario.a.nunes12-Aug-13 3:36 
GeneralMy vote of 5 Pin
Farhan Ghumra17-Jun-12 23:09
professionalFarhan Ghumra17-Jun-12 23:09 
QuestionVisual Studi 2010 Pin
Mehdi Keramati8-Jul-11 14:02
Mehdi Keramati8-Jul-11 14:02 
AnswerRe: Visual Studi 2010 Pin
zwalton1-Sep-11 8:54
zwalton1-Sep-11 8:54 
Generaldude great work Pin
bheeem26-Jan-11 2:36
bheeem26-Jan-11 2:36 
Questionhow can i protect the page from user without user in membership asp.net with c sharp Pin
desaihardikj@gmail.com9-Dec-10 14:09
desaihardikj@gmail.com9-Dec-10 14:09 
QuestionHaving a problem in web.config Pin
dastarr18-Jun-10 15:00
dastarr18-Jun-10 15:00 
QuestionAdmin Account does not bring me to admin/default.aspx [modified] Pin
dastarr18-Jun-10 8:44
dastarr18-Jun-10 8:44 
GeneralMy vote of 1 Pin
Andrey Mazoulnitsyn9-May-10 1:17
Andrey Mazoulnitsyn9-May-10 1:17 
QuestionTemplate Pin
khaksaraziz13-Apr-10 18:20
khaksaraziz13-Apr-10 18:20 
GeneralThanks Pin
the_bg14-Feb-10 9:11
the_bg14-Feb-10 9:11 
QuestionVB Version? Pin
the_bg13-Feb-10 18:56
the_bg13-Feb-10 18:56 
GeneralExactly what I was looking for Pin
slaphappy19751-Jan-10 15:34
slaphappy19751-Jan-10 15:34 
GeneralThanks Pin
RkHrd27-Nov-09 13:08
RkHrd27-Nov-09 13:08 
GeneralError: Could not load file or assembly 'System.Data.Entity' Pin
Behzad Ebrahimi24-Aug-09 18:52
Behzad Ebrahimi24-Aug-09 18:52 
GeneralRe: Error: Could not load file or assembly 'System.Data.Entity' Pin
Stephen Inglish26-Aug-09 9:32
Stephen Inglish26-Aug-09 9:32 
GeneralRe: Error: Could not load file or assembly 'System.Data.Entity' Pin
benleo9-Aug-10 14:42
benleo9-Aug-10 14:42 
upgrade to donet framework 3.5 to sp1
GeneralDude, awesome. Pin
martenc15-Jul-09 14:54
martenc15-Jul-09 14:54 
GeneralRe: Dude, awesome. Pin
Stephen Inglish16-Jul-09 6:15
Stephen Inglish16-Jul-09 6:15 
GeneralJust starting C# - Moving from vbscript Pin
Steve Krile14-Jul-09 1:55
Steve Krile14-Jul-09 1:55 
GeneralRe: Just starting C# - Moving from vbscript Pin
Stephen Inglish16-Jul-09 6:14
Stephen Inglish16-Jul-09 6:14 
GeneralRe: Just starting C# - Moving from vbscript Pin
Steve Krile16-Jul-09 6:17
Steve Krile16-Jul-09 6:17 
GeneralRe: Just starting C# - Moving from vbscript Pin
Stephen Inglish17-Jul-09 2:16
Stephen Inglish17-Jul-09 2:16 
GeneralThanks for this Template Pin
musacj2-Jul-09 2:19
musacj2-Jul-09 2:19 
GeneralGood Job! Pin
Moim Hossain26-Jun-09 10:44
Moim Hossain26-Jun-09 10:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.