Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [431]

By Root 5501 0
Login Control


The Login control is a composite control that provides all the common user interface elements of a login form. To use it, you simply drop the control from the toolbox onto the Web form, or you just type the following code:

The Login control also has optional user-interface elements for functions such as password reminders, new user registration, help links, error messages, and a custom action used in the case of a successful login. When you drop the control onto a Visual Studio form, the AutoFormat verb lets you choose among a few predefined styles, as shown in Figure 19-9.

Figure 19-9. The predefined styles of the Login control.

The appearance of the control is fully customizable through templates and style settings. All user-interface text messages are also customizable through properties of the class.

The Programming Interface of the Control


The control is modularized, and each constituent part can be individually customized. The parts include the Username and Password text boxes, the Submit button, the button to create a new user, the Remember Me check box, and instructions with guidance to the user.

If you don’t like the standard user interface of the control, you can define your own template too:

...

Your template can include new elements, and you can recycle default components. To do the latter, you should use the same ID for the controls as in the default template. To simplify this operation, right-click on the control in the Visual Studio designer, choose Convert To Template, and switch to the Source view. The markup you see is the default template of the control expressed as ASP.NET code. Use it as a starting point for creating your own template.

Events of the Control


The Login control fires the server events listed in Table 19-15.

Table 19-15. Events of the Login Control

Event

Description

Authenticate

Fires when a user is authenticated.

LoggedIn

Fires when the user logs in to the site after a successful authentication.

LoggingIn

Fires when a user submits login information but before the authentication takes place. At this time, the operation can still be canceled.

LoginError

Fires when a login error is detected.

In most common cases, though, you don’t need to handle any of these events, nor will you likely find it necessary to programmatically access any of the numerous properties of the control.

The most common use for the Login control is to use it as a single-control page to set up the user interface of the login page for use with Forms authentication. The control relies entirely on the membership API (and the selected provider) to execute standard operations, such as validating credentials, displaying error messages, and redirecting to the originally requested page in the case of a successful login.

If you have a provider with custom capabilities that you want to be reflected by the Login control, you need to modify the layout to add new visual elements bound to a code-behind method. In the code-behind method, you invoke the custom method on the custom provider.

The LoginName Control


The LoginName control is an extremely simple but useful server control. It works like a sort of label control and displays the user’s name on a Web page:

The control captures the name of the currently logged-in user from the User intrinsic object and outputs it using the current style. Internally, the control builds a dynamic instance of a Label control, sets fonts and color accordingly, and displays the text returned by the following expression:

string name = HttpContext.Current.User.Identity.Name;

The LoginName control has a pretty slim programming interface that consists of only one property—FormatString. FormatString defines the format of the text to display. It can contain only one placeholder, as shown here:

myLogin.FormatString = "Welcome, {0}";

If Dino is the

Return Main Page Previous Page Next Page

®Online Book Reader