Programming Microsoft ASP.NET 4 - Dino Esposito [432]
The LoginStatus Control
The LoginStatus control indicates the state of the authentication for the current user. Its user interface consists of a link button to log in or log out, depending on the current user logon state. If the user is acting as an anonymous user—that is, he or she never logged in—the control displays a link button to invite the user to log in. Otherwise, if the user successfully passed through the authentication layer, the control displays the logout button.
Setting Up the LoginStatus Control
The LoginStatus control is often used in conjunction with the LoginName control to display the name of the current user (if any), plus a button to let the user log in or out. The style, text, and action associated with the button changes are conveniently based on the authentication state of the user.
The following code creates a table showing the name of the current user and a button to log in or log out:
To detect whether the current user is authenticated and adapt the user interface, you can use the IsAuthenticated property of the User object:
void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
// Adjust the UI by outputting some text to a label
Msg.Text = "Enjoy more features";
else
Msg.Text = "Login to enjoy more features.";
}
The Programming Interface of the Control
Although the LoginStatus control is quite useful in its default form, it provides a bunch of properties and events you can use to configure it. The properties are listed in Table 19-16.
Table 19-16. Properties of the LoginStatus Control
Property
Description
LoginImageUrl
Gets or sets the URL of the image used for the login link.
LoginText
Gets or sets the text used for the login link.
LogoutAction
Determines the action taken when a user logs out of a Web site. Possible values are Refresh, Redirect, and RedirectToLoginPage. Refresh reloads the current page with the user logged out. The other two values redirect the user to the logout page or the login page, respectively.
LogoutImageUrl
Gets or sets the URL of the image used for the logout button.
LogoutPageUrl
Gets or sets the URL of the logout page.
LogoutText
Gets or sets the text used for the logout link.
The control also features a couple events: LoggingOut and LoggedOut. The former fires before the user clicks to log off. The latter is raised immediately after the logout process has completed.
The LoginView Control
The LoginView control allows you to aggregate the LoginStatus and LoginName controls to display a custom user interface that takes into account the authentication state of the user as well as the user’s role or roles. The control, which is based on templates, simplifies creation of a user interface specific to the anonymous or connected state and particular roles to which they are assigned. In other words, you can create as many templates as you need, one per state or per role.
The Programming Interface of the Control
Table 19-17 lists the properties of the user interface of the LoginView control.
Table 19-17. Properties of the LoginView Class
Property
Description
AnonymousTemplate
Gets or sets the template to display to users who are not logged in to the application.
LoggedInTemplate
Gets or sets the template to display to users who are logged in to the application.
RoleGroups
Returns the collection of templates defined for the supported roles. Templates can be declaratively specified through the Note that the LoggedInTemplate template is displayed only to logged-in users who are not members of one of the role groups specified in the RoleGroups property. The template (if any) specified in the The LoginView