|
Home Preview the Book Chapter 15, TOC Part 24
Previous:
Personalizing Web Pages
Next:
Storing Visitor Information in Persistent Cookies
Getting the User Name from an Identity Object
The index.aspx page occupies the top of the page hierarchy, so it does not use the breadcrumbs custom control. The u_crumb_welcome.ascx user control appears in place of the breadcrumbs control. If the visitor is logged in, the control greets the visitor by name and offers a logout link. If the visitor is not logged in, the control displays a generic greeting and no link. (The visitor can log in directly from the index.aspx page if he or she chooses.)
Listing 15-9 contains the ASCX code for this user control. As with all the VB Snippets controls, the text strings are localized.
Listing 15-9. The u_crumb_welcome.ascx User Control
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="u_crumb_welcome.ascx.vb"
Inherits="Chapter_15.u_crumb_welcome"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<p class="lfloatpad">
<span class="crumb"><img border="0" src="images/arrow.gif"
width="6" height="10">
<%=rm.GetString("Welcome")%>,
<span id="txtName" runat="server"></span>,
<%=rm.GetString("toVbSnippets")%>
</span>
</p>
<p class="rfloatpad" id="pLogout" runat="server">
<a href="index.aspx?action=logout"
class="logout"><%=rm.GetString("LogOut")%></a>
</p>
Notice how the logout link works. If the visitor clicks it, the control links back to index.aspx with a query string of action=logout. In fact, the logout link in the breadcrumbs custom control works the same way. The index.aspx page includes the following code at the top of its Page_Load procedure:
Dim sAction As String = Request.Params("action")
If sAction = "logout" Then
Dim oVisitor As CVisitor = New CVisitor(Me)
oVisitor.LogOut()
Response.Redirect("index.aspx")
End If
The Logout method of the CVisitor class, which we present shortly, updates the database with current information and relinquishes the visitor’s authentication. The Response.Redirect method redisplays the index.aspx page in its generic form.
The code in Listing 15-10 shows how the user control in the index.aspx page personalizes its message. If the Page.User.Identity.IsAuthenticated property is True, the control displays the Page.User.Identity.Name property value. This property returns the Username value supplied when the visitor is authenticated.
Listing 15-10. The u_crumb_welcome.ascx.vb User Control Code
Option Strict On
Imports System.Resources
Public MustInherit Class u_crumb_welcome
Inherits System.Web.UI.UserControl
Protected WithEvents pLogout As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents txtName As System.Web.UI.HtmlControls.HtmlGenericControl
[Designer generated code omitted]
Protected rm As ResourceManager
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
rm = New ResourceManager("Chapter_15.strings", GetType(index).Assembly)
If Page.User.Identity.IsAuthenticated Then
'Get name and display logout option
txtName.InnerText = Page.User.Identity.Name
Me.pLogout.Visible = True
Else
'Visitor is not logged in
txtName.InnerText = rm.GetString("friend")
Me.pLogout.Visible = False
End If
End Sub
End Class
The control uses the Visible property to control whether ASP.NET generates the HTML for the logout link. If the visitor is logged in, the link is displayed; if not, the link is suppressed.
The authentication used to control the personalization of VB Snippets occurs in the CVisitor class. We present that class now.
Previous:
Personalizing Web Pages
Next:
Storing Visitor Information in Persistent Cookies
Home Preview the Book Chapter 15, TOC Part 24
|