Updating the client name using Windows authentication

I am trying to get the username and client computer name of a person logged into a computer on our intranet in ASP.NET. It's just for logging purposes. I am getting username "System.Security.Principal.WindowsIdentity.GetCurrent (). Name", the problem is whoever is accessing this site showing the same username (that is, the name of the server where I deployed my application) for all. Please help. I am using windows authentication mode in web.config.

0


a source to share


2 answers


The username can be specified as described by @Mehrdad . For the user machine name, you can use the HttpRequest object like this:

if(Request.IsAuthenticated)
    string userName = Request.LogonUserIdentity.Name;
string machineAddress = Request.UserHostAddress;
string machineName = Request.UserHostName;

      

(edit)

In my web.config file I am using this line:

<system.web>
    <authentication mode="Windows"/>
</system.web>

      

In default.aspx.cs I am using this:



public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();

        if (Request.IsAuthenticated)
        {
            sb.AppendFormat("User Name: {0}<br/>", Request.LogonUserIdentity.Name);
        }
        else
        {
            sb.Append("Request not authenticated");
        }

        sb.AppendFormat("Machine Address: {0}<br/>", Request.UserHostAddress);
        sb.AppendFormat("Machine Name:    {0}<br/>", Request.UserHostName);

        lblTest.Text = sb.ToString();
    }
}

      

This outputs the following output:

Username: HPAS \ amantur

Machine address: 127.0.0.1

Machine name: 127.0.0.1

+2


a source


The code you use will get WindowsIdentity

associated with the current thread (which is the ASP.NET ID running). Unless you are impersonation based on customer ID that won't work. You should use this:



HttpContext.Current.User.Identity.Name

      

+1


a source







All Articles