How can I get the client machine username in ASP.net?

In the Localhost, my username is "MTA" when this code is called:

string opl = HttpContext.Current.User.Identity.Name.ToString();
TextBox1.Text = opl.Substring(opl.IndexOf(@"\") + 1);

OR this code:

string opl = System.Environment.UserName.ToString();
TextBox1.Text = opl.Substring(opl.IndexOf(@"\") + 1);

But after publishing and accessing a website with Windows Server. My username is now "SRVCMAN".

+3
source share
3 answers

IIS, Authentication Anonymous Authentication Windows Authentication

:

var ident = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
If(!ident.IsAnonymous && ident.IsAuthenticated)
{
  var loginUsername = ident.Name;
}
+4
    // will return the host name making the request

    string s = Request.ServerVariables["REMOTE_HOST"] ;
-----------------------------------------------------------------
    // will return the computer name

    string s = Request.ServerVariables["SERVER_NAME"] ;
-----------------------------------------------------------------
   //will return Windows account for the user.

    string s = Request.ServerVariables["AUTH_USER"] ;
-----------------------------------------------------------------

I think you are trying to get this information:

IIS Server Variables

+4
source

All Articles