How do I get the computer account from which a user signed in to my application?
How to get the user account with which he accesses the application in case Form authentication
.
I am using the following method, but it is not getting the required data:
protected string[] TrackUser()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string IP = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
string compName = (Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName);
string account = Request.ServerVariables["AUTH_USER"];
string[] user_network_data = new string[3];
if (!string.IsNullOrEmpty(IP))
{
string[] addresses = IP.Split(',');
if (addresses.Length != 0)
{
IP = addresses[0];
}
}
else
{
IP = context.Request.ServerVariables["REMOTE_ADDR"];
}
user_network_data[0] = IP;
user_network_data[1] = compName;
user_network_data[2] = account;
return user_network_data;
}
+3
source to share
1 answer
I think you are just grabbing the wrong original information from the request. Try the following:
string IP = context.Request.UserHostAddress;
string compName = context.Request.UserHostName;
string account = context.Request.LogonUserIdentity.Name;
I think the rest of your code should work just fine as long as you have the correct data to start with.
+2
source to share