ASP.NET Tip: Using the LoginName and LoginStatus Controls

Besides the new LoginView control that Microsoft added to ASP.NET 2.0, you could use a couple of handy controls in conjunction with your web site security: LoginName and LoginStatus.

The LoginName control simply displays the user name if the user is logged in. If the user isn't logged in, nothing is rendered to the page. You can combine this with the LoginView control as follows:

<asp:LoginView ID="ctlRightNavbar" runat="server">
   <AnonymousTemplate><a href="/login.aspx" class="navbar">
      User Login</a></AnonymousTemplate>
   <LoggedInTemplate>
      <a href="/logout.aspx" class="navbar">
         Logout <asp:LoginName ID="ctlUserName" Runat="server" /></a>
   </LoggedInTemplate>
</asp:LoginView>

If the user is logged in as Eric, the text Logout Eric will be displayed. The only downside to this control is that if you are not storing the actual user name in User.Identity.Name, the control won't get you anything. In some of my applications, I store the user's ID number in User.Identity.Name, so the result for my application is Logout 123, for instance.

The LoginStatus control is designed to do the same thing as the LoginView control. Its default behavior is to show a link to a login page or logout page, depending on the user's authentication status. It has properties for images and text to be shown in each state, as well as events to which you can add code. Here's LoginStatus code that does the same thing as the previous LoginView example:

<asp:LoginStatus ID="ctlLogin"
                 runat="server"
                 LoginText="User Login"
                 LogoutText="User Logout"
                 LogoutPageUrl="/logout.aspx"
                 LogoutAction="Redirect" />

Each of the control's properties lets you control each behavior. The property that is "missing" is the LoginPageUrl, but that property is automatically pulled from your Web.config file to determine the login page.

The LogoutAction property is interesting in that you can choose to simply refresh the existing page after logout (use a value of Refresh or leave it empty), go to the logout page (use the value of Redirect), or bounce back to the login page (use a value of RedirectToLoginPage).

The other interesting thing is that this control resolves the issue of being able to see unauthenticated content after logout; the LoginView control didn't do that.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds