1

This is my default.aspx file

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="Default2" %>

  <!DOCTYPE html>

   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
   <title></title>
   </head>
    <body>
   <form id="form1" runat="server">
   <div>
    Welcome to sample website:)
   </div>
     <p>
        &nbsp;</p>
    <p>

        <asp:LoginName id="LoginName1" runat="server" FormatString="Welcome,{0}" />

    </p>
    <p>
        &nbsp;</p>
</form>
</body>
</html>

I am able to display the doaminname\username but I need to display only the username . i am confused how do I go ahead with this.Can anyone help me with this?

1 Answers1

0

Add the below code in your page_load

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
if (windowsIdentity == null)
    throw new InvalidOperationException("WindowsIdentity is null");
string nameWithoutDomain = windowsIdentity.Name.Split('\\').Last();

LoginName1.Text = String.Format("Welcome,{0}", nameWithoutDomain)

And if you want a helper class you can refer to this Built-in helper to parse User.Identity.Name into Domain\Username

Community
  • 1
  • 1
Sailor
  • 183
  • 5