As part of your application, you need to establish the user's identity so that the application can perform actions — such as showing or hiding information — based on who the user is. To get the user's identity, you have the user log in.
In this walkthrough, you will add a link on the home page that takes users to a login page, and then you will create the login page.
To create a home page with a login button
Open or switch to the Default.aspx page of your site. (If you do not have a Default.aspx page, you can add one or use a different page.)
Switch to Design view.
Type static text such as Welcome to our site and, in the Formatting toolbar, use the Block Format drop-down list to format the text as Heading 1.
From the Login group of the Toolbox, drag a LoginStatus control onto the page.
By default, the LoginStatus control is rendered as a link. When users click it, the application displays a login page. You can now create the login page.
To create a login page
In Solution Explorer, right-click your Web application and select Add New Item. Add a Web Form named Login.aspx to your site.
Note: |
|---|
For this walkthrough, the page must be named
Login.aspx. By default, forms authentication is configured to work with a page with this name. Although you will not do so in this walkthrough, you can change the default login page — the page to which users are redirected — in the Web.config file.
|
In the Login.aspx page, switch to Design view.
From the Login group of the Toolbox, drag a Login control onto the page.
The Login control is a single control that will prompt the user for credentials and validate them.
Displaying Login Errors
The Login control includes validation to help users enter correct information. For example, if a user skips the password, a validator control displays an asterisk (*) next to the Password box. You can provide more complete information for login errors by adding a ValidationSummary control to the page.
To display detailed login errors
Displaying Information for Logged-In Users
You will now modify the home page to customize the display depending on whether the user is logged in. Anonymous users will see a generic message inviting them to log in. Logged-in users will see a message that welcomes them by their logged-in name.
To customize the display for logged-in users
Switch to or open the Default.aspx page.
From the Login group of the Toolbox, drag a LoginView control onto the page.
The LoginView control is displayed with its AnonymousTemplate template open. This template allows you to define the content that users will see before they have logged in.
Click the edit area of the LoginView control to activate editing.
In the edit area of the LoginView control's AnonymousTemplate template, type You are not logged in. Click the Login link to sign in.
On the LoginView Tasks panel, in the Views list, click LoggedInTemplate. If you do not see the LoginView Tasks panel, right-click the heading of the LoginView control and select Show Smart Tag.
You are now defining the content that will be displayed to users who have already logged in.
Click the edit area of the LoginView control to activate editing, and then type You are logged in. Welcome,.
From the Login group of the Toolbox, drag a LoginName control into the template after the text.
Testing Login
You can now test the login capability of your application.
To test login
In Solution Explorer, right-click Default.aspx and click Set As Start Page.
This configures the Web site so that when you run the site, the Default.aspx page appears first.
Press CTRL+F5 to run the Web site.
The home page (Default.aspx) appears in the browser, showing the Login link and the generic message.
Click the Login link.
The login page you created is displayed.
Type the login name of the user you created earlier in the walkthrough, and then click Log In. (Do not yet enter a password.)
An asterisk (*) is displayed next to the Password box, and an error message is displayed in the ValidationSummary control.
Type both a user name and password and then click Log In.
If you entered correct credentials, you are returned to the home page. The page now displays a Logout link, your user name, and the welcome message that you defined for the logged-in user.
Close the browser.