Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
Often we want to display different content based on whether the user is logged on. If the visitor is not logged on to the site, we might want to display the Login control; however, if the user has already been authenticated, in place of the Login control, we might want to display a short message like Welcome back username, where username is the name of the logged-on user. As we saw in the preceding section, the Login control can be conditionally displayed based on the user's authentication status via the VisibleWhenLoggedIn property. Although setting this property to False will indeed hide the Login control from authenticated users, it doesn't provide a mechanism to replace the login control with a customized message. To accomplish that, we'll need to use the LoginView Web control. Go to Default.aspx and add a LoginView control to the page. As the control's smart tag shows, the LoginView control provides two views: Anonymous Template, which is shown for unauthenticated users; and Logged In Template, which is shown for logged-in users. To add content that should appear only when the visitor is authenticated or not, simply add the content to the appropriate view. Because we want Default.aspx to show a Login control for unauthenticated users, switch to the Anonymous Template view and then drag and drop a Login control from the Toolbox into the LoginView control. Next, switch to the LoginView control's Logged In Template. Whatever static HTML markup or Web controls we add here will appear for authenticated users. Begin by putting your mouse cursor inside the Logged In Template and click to give focus. Then type in the text Welcome back. Finally, drag the LoginName control from the Toolbox into the Logged In Template, preceding the text you just added (see Figure 20.13). Figure 20.13. The LoginName control has been added to the LoginStatus control's Logged In Template.
As you may have guessed, the LoginName control displays the username of the logged-on user. If an anonymous user visits a page, the LoginName control displays nothing. Figure 20.14 shows Default.aspx when viewed by an anonymous user, whereas Figure 20.15 shows the page when visited by a logged-on user. Figure 20.14. For anonymous users, the Login control is shown.
Figure 20.15. After Jisun has logged on, she sees a "Welcome back" message.
|
Категории