asp.net c# tutorials and examples code for web developers

How to show login status in asp.net

LoginStatus Web Server Control
.net framework LoginStatus Class allow us to detect the user's authentication state and toggles the state of a link to log in to or log out of a web site. to allow to users to log in an asp.net application that uses forms authentication, we can use the LoginStatus server control to detect an user authentication status.

when this control found an user is not authenticated then it display a button to move user to the login page. if the control found a login user then it display a logout button for this user to log out from the web site.

we can customize the default look and feel of LoginStatus control by setting its various properties such as BackColor, ForeColor, BorderStyle, BorderColor, BorderWidth, CssClass, Font, Height, Width etc.

LoginStatus control's LoginImageUrl property allow us to set an url of the image used for login page link. LoginText property set the text display for the login link.

LogoutText property set a text used for the log out link. LogoutPageUrl property set an url for the logout page. LogoutImageUrl property set an image url to display as logout button. LogoutAction property allow us to set a value that determine the action taken when a user logs out of a web site with LoginStatus server control.

LoginName is an asp.net web server control that display an user's login name if the user has logged in using asp.net membership. if the web site uses integrated windows authentication then LoginName control displays the user's windows account name.

the following asp.net example code demonstrate us how can we show an user's login status in asp.net application using LoginStatus web server control.
Login.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>LoginStatus Control Example: How to show login status in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>My web site</h2>
        <asp:Login ID="Login1" runat="server" BackColor="#FFFBD6" BorderColor="#FFDFAD" 
            BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" 
            Font-Size="0.8em" ForeColor="#333333" TextLayout="TextOnTop">
            <TextBoxStyle Font-Size="0.8em" />
            <LoginButtonStyle BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" 
                BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" 
                ForeColor="#990000" />
            <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
            <TitleTextStyle BackColor="#990000" Font-Bold="True" Font-Size="0.9em" 
                ForeColor="White" />
        </asp:Login>
    </div>
    </form>
</body>
</html>
Default.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>LoginStatus control example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Show Login Status</h1>
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
        <br /><br />
        <asp:LoginName ID="LoginName2" runat="server" FormatString="Hi {0}!" Font-Size="XX-Large" ForeColor="Crimson" />
    </div>
    </form>
</body>
</html>
Related asp.net example