Click here to Skip to main content
6,630,289 members and growing! (23,175 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Beginner License: The Code Project Open License (CPOL)

Fully configured ASP.NET Membership Website Template

By Stephen Inglish

A pre-configured ASP.NET website containing a Master page, error handling, login / logout, and other boilerplate new project code.
Javascript, CSS, XHTML, C# 1.0, C# 2.0, C# 3.0.NET 3.5, ASP.NET, Visual Studio (VS2008), Dev
Version:6 (See All)
Posted:26 Jun 2009
Updated:7 Jul 2009
Views:13,458
Bookmarked:72 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.94 Rating: 4.74 out of 5

1

2
1 vote, 9.1%
3
1 vote, 9.1%
4
9 votes, 81.8%
5

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.

<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;*()_+])
<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:

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.

<%@ 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.

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)

About the Author

Stephen Inglish


Member

Occupation: Software Developer
Company: Sogeti USA
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralError: Could not load file or assembly 'System.Data.Entity' PinmemberBehzad Ebrahimi19:52 24 Aug '09  
GeneralRe: Error: Could not load file or assembly 'System.Data.Entity' PinmemberStephen Inglish10:32 26 Aug '09  
GeneralDude, awesome. Pinmembermartenc15:54 15 Jul '09  
GeneralRe: Dude, awesome. PinmemberStephen Inglish7:15 16 Jul '09  
GeneralJust starting C# - Moving from vbscript PinmemberSteve Krile2:55 14 Jul '09  
GeneralRe: Just starting C# - Moving from vbscript PinmemberStephen Inglish7:14 16 Jul '09  
GeneralRe: Just starting C# - Moving from vbscript PinmemberSteve Krile7:17 16 Jul '09  
GeneralRe: Just starting C# - Moving from vbscript PinmemberStephen Inglish3:16 17 Jul '09  
GeneralThanks for this Template Pinmembermusacj3:19 2 Jul '09  
GeneralGood Job! PinmemberMoim Hossain11:44 26 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jul 2009
Editor: Smitha Vijayan
Copyright 2009 by Stephen Inglish
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project