How to show login name in asp.net

Show login name in asp.net
LoginName is an asp.net web server control. LoginName control allow us to display a user's login name if the user has logged in using asp.net membership. If site uses integrated windows authentication, the loginname control display the user's windows account name.

LoginName class allow us to display System.Web.UI.Page.User.Identity.Name property value. LoginName class exists in System.Web.UI.WebControls namespace.

LoginName control has many useful built in properties to provide better user experience such as AccessKey, BackColor, BorderColor, Attributes, BorderStyle, BorderWidth, CssClass, Font, ForeColor, FormatString, HasAttributes, Height, IsEnabled, Page, Parent, SkinID, Style, TabIndex, TagKey, ToolTip, ViewState, Visible, Width etc.

LoginName control has few events such as DataBinding, Disposed, Int, Load, PreRender and Unload.

LoginName.FormatString property allow us to provide a format item string to display. FormatString property value data type is System.String. this value represents a string containing format items for displaying the user's name. The default value of FormatString property is "{0}".

This FormatString property contains a standard text format string. the string "(0)" indicates where in the string the user's name is inserted. FormatString property throw FormatException exception, if the format string is not valid.

The following asp.net c# example code demonstrate us how can we display logged in user's name in a web page using LoginName server control in an asp.net application.
Login.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>LoginName Example: How to show login name in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" 
            BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" 
            Font-Size="0.8em" ForeColor="#333333">
            <TextBoxStyle Font-Size="0.8em" />
            <LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" 
                BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
            <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
            <TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em" 
                ForeColor="White" />
        </asp:Login>
    </div>
    </form>
</body>
</html>
Default.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Using LoginName Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Wellcome my site</h1>
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
        <br />
        <asp:LoginName ID="LoginName1" runat="server" FormatString="{0} logged in." Font-Size="Medium" ForeColor="BlueViolet" />
        <br /><br />
        <asp:LoginName ID="LoginName2" runat="server" FormatString="Hi {0}!" Font-Size="XX-Large" ForeColor="Crimson" />
    </div>
    </form>
</body>
</html>
More asp.net examples