Creating ASP.NET Login Web Pages and Basic Configuration

This is another important tutorial on how to create a login page in ASP.NET 3.5. This is a part of a series of user management related tutorials in ASP.NET. You will learn how to create and configure a page that allows members to log into your website.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 39
August 10, 2010
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

If you have not read those tutorials, it is highly recommended that you read those first in order to understand this material easily. These are the links:

Introduction to ASP.NET 3.5 User Account Management explains the principles of user account management and gets you started with the basics.

ASP.Net 3.5: User Account Creation for Your Website shows you how to allow users to register as members in your website while maintaining security.

This article has two main learning objectives which you can expect to know by the end of this tutorial. First, you will know how to create a login page for your ASP.NET website. Second, you will be able to perform basic configuration of ASP.NET login web controls.

Example: creating an ASP.NET login web page

For example, say you're starting from scratch in creating a login web page in your ASP.NET website. You're the first user of the website, as well as the administrator. What are the steps you need to take to get started? The following is the flow chart of the implementation:

According to the flow chart, you need to create your ASP.NET website first. This tutorial uses Visual Web Developer Express 2008 as the editor and Visual Basic as the language. Name your project aspnetloginexercise.

The website is then created, with the following initial files:

  • Default.aspx
  • Default.aspx.vb
  • web.config
  • App_Data

The flow chart above can be used to create a variety of user-management enabled ASP.NET websites in either small or large domains. Once you have a fully working login web page onto which an administrator can log in, you can start letting other users log into your website, or register them using the CreateUserWizard web control as discussed in this tutorial.

Configuring the Website Administration Tool

Now that you have created your ASP.NET website, the next step is to enable “Form-based authentication.” Details of this process are discussed in this tutorial.

To review, the main purpose of enabling form-based authentication is to allow the use of web forms for logging in. To do this, follow the steps below:

  1. Go to the source code view of Default.aspx.
  2. In Visual Web Developer menu, go to Website -> ASP.NET configuration.
  3. The Website Administration tool will then be launched in the browser.
  4. On the front page, click “Security.”
  5. Under “Users,” click “Select authentication type.”
  6. Under “How will users access your site?” click the radio button “From the Internet.”
  7. Click “Done” (located at the bottom).

Now that you have enabled “form based authentication” using a web page, you need to create the first website user -- namely, yourself. Follow the steps below (still in the Website Administration Tool):

  1. Click “Create User” under “Users.”
  2. You need to enter information as completely as possible. Note your username and password; write it down on a piece of paper, because you will need that later. Click “Create User” when done. Do not forget to check “Active user.”
  3. If you see the message “Complete; your account has been successfully created,” click Continue.
  4. However, you only need to add one user for now, so click the “Security” link in the menu.

You need to create the “Administrator” role and assign your newly-created username as the website administrator. To do this:

  1. Under Roles, click “Enable roles.”
  2. Click “Create or Manage roles.”
  3. Under “Create New role,” enter Administrator and then click “Add Role.”
  4. Under “Add/Remove Users,” click “Manage.”
  5. You need to type the username you created earlier, and then click “Find User.”
  6. Once you see your username, mark the checkbox under “User Is In Role” checked. This will assign your username to the role of administrator.
  7. Close the browser window of the Website Administration tool. The basic configuration has been completed.

Create login.aspx

The default login page in ASP.NET websites is called login.aspx. You need to create this page; its exact name should be “login.aspx.” To do this, go through the following steps.

Step 1. In Visual Web Developer, go to View -> Solution Explorer.

Step 2. In Solution Explorer, right click on your website local path (e.g. E:\aspdotnetprojects\aspnetloginexercise\) and click “Add New Item.” Under “Visual Studio Installed templates” select “Web Form.”

Step 3. Change the name from Default2.aspx to login.aspx.

Set the language to “Visual Basic” and check “Place code in separate file.”

Step 4. Click “Add.”

Step 5. Go to File -> Save All.

You will then see login.aspx added as the new ASP.NET web page in your root directory, in addition to Default.aspx.

You need to accomplish two important things on login.aspx. First, you need to show a login web form to anonymous users (using “AnonymousTemplate”). Second, you need to present a welcome message for logged-in users (using the “LoggedInTemplate”).

To do this, you need to use the “LoginView” web control (View -> Toolbox -> Login -> LoginView).

Step 1. Go to the source code view of login.aspx.

Step 2. Click and drag the “LoginView” web control to between the <form><div> and </form></div> tags.

Step 3. Go to the design view.

Step 4. Click the web control and the > symbol. You will then see two templates in a drop down; the default shown is the “AnonymousTemplate” while the other is the“Logged InTemplate.”

Make sure the “AnonymousTemplate” view is set first.

Step 5. Using the “AnonymousTemplate,” click and drag the “Login” web control (View -> Toolbox -> Login -> Login) to the box inside “LoginView1.” It should look like this after dragging:

Step 6. Now change the views to “LoggedInTemplate” by clicking > arrow and then selecting “LoggedInTemplate.”
 
It will then change to:

You will need to enter a welcome text for the logged-in user, as well as set it up to display the username.

Example:

Hi, [UserName]…Welcome back to the website!

Note:
The web control [UserName] can be created by dragging LoginName from the toolbox (View -> Toolbox -> Login -> LoginName) to the box inside LoginView.

Screen shot:

The configuration of login.aspx is complete. For reference, below is the complete login.aspx source code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="login.aspx.vb" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LoginView ID="LoginView1" runat="server">
            <LoggedInTemplate>
                Hi,
                <asp:LoginName ID="LoginName1" runat="server" />
                ...Welcome back to the Website!
            </LoggedInTemplate>
            <AnonymousTemplate>
                <asp:Login ID="Login1" runat="server">
                </asp:Login>
            </AnonymousTemplate>
        </asp:LoginView>
    </div>
    </form>
</body>
</html>

Create a Login Link from the Home Page

Finally, at the home page (Default.aspx), you can add a login link which is dynamically controlled when a user is or is not logged in.

If the user is NOT logged in, the “login” hyperlink will be shown; otherwise, the “logout” link will be displayed.

This is normally what you see in standard ASP.NET websites. To do this, take the following steps. 

Step 1. Add some text on your home page.

Example:

<form id="form1" runat="server">
<div>
Hi, this is your homepage. IF you are logged in to my website, you will see the logout link below, otherwise a login link will be shown.   
</div>
</form>

Step 2. Click and drag the LoginStatus web control (View -> Toolbox -> Login -> LoginStatus) next to the text paragraph. So it will now look like this:

<form id="form1" runat="server">
<div>
Hi, this is your homepage. IF you are logged in to my website, you will see the logout link below, otherwise a login link will be shown.   
<br />
<br />
<asp:LoginStatus ID="LoginStatus1" runat="server" />
</div>
</form>

Step 3. Go to the Design View, click the > of “Login.” You should see two templates:

a. Logged Out

b. Logged In

Step 4. The final Default.aspx source code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Hi, this is your homepage. IF you are logged in to my website, you will see the logout link below, otherwise a login link will be shown.   
    <br />
    <br />
    <asp:LoginStatus ID="LoginStatus1" runat="server" />
    </div>
    </form>
</body>
</html>

Test in Web Browser

To test in the browser, go to Default.aspx, then File -> View in Browser. What you will see should look like the screen shot below:

Since the user is still not logged in, the Logged Out template of the LoginStatus web control is shown, so you will see the “Login” link below the text.

Clicking the “Login” link automatically takes you to the login page. If you are logged in, the “login” link you saw earlier on the home page changes to “Logout.”

Now that you have created a basic login page, you can also incorporate or create a new page to sign up new users (using CreateUserWizard). Also, you do not need to create a new database for your applications, since ASP.NET has already created a database  for you, named ASPNETDB.MDF, which you can also use for your applications (aside from user management purposes).

blog comments powered by Disqus
.NET ARTICLES

- .Net 4.5 Brings Changes
- Understanding Events in VB.NET
- Objects, Properties, Events and Methods in V...
- Install Visual Web Developer Express 2010
- Microsoft Gadgeteer an Open Source Alternati...
- Best DotNetNuke Modules
- Facebook Image Viewer in Visual Basic
- Murach`s ADO.NET 4 Database Programming with...
- 5 Must Have Visual Studio 2010 Extensions
- Dynamic Web Applications with ASP.NET Mono u...
- PDFSharp: HTML to PDF in ASP.NET 3.5 using V...
- Using the PDFSharp Library in ASP.NET 3.5 wi...
- Sending Email in ASP.NET 3.5 using VB.NET wi...
- ASP.NET 3.5 Role Based Security and User Aut...
- Creating ASP.NET Login Web Pages and Basic C...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials