I'm attempting to retrieve the user name and client machine name of the person logged on to a computer on our intranet in ASP.NET. This is just for logging purposes. I retrieve the user name "System.Security.Principal.WindowsIdentity.GetCurrent().Name", problem is whoever accessing this site showing the same username (that is server name where I have deployed my application) for all. Please help. I am using windows authentication mode in web.config.
+1
A:
The code you are using will get the WindowsIdentity
associated with the current thread (which is the identity ASP.NET is running on). Unless you are impersonating based on client user identity that won't work. You need to use this:
HttpContext.Current.User.Identity.Name
Mehrdad Afshari
2009-05-13 10:15:18
A:
The name of the user can be have as described by @Mehrdad. For the name of the user's machine you can use HttpRequest object like this:
if(Request.IsAuthenticated)
string userName = Request.LogonUserIdentity.Name;
string machineAddress = Request.UserHostAddress;
string machineName = Request.UserHostName;
(edit)
In the web.config file I'm using this line:
<system.web>
<authentication mode="Windows"/>
</system.web>
In the default.aspx.cs I'm 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 is proucing following output:
User Name: HPAS\amantur
Machine Address: 127.0.0.1
Machine Name: 127.0.0.1
TheVillageIdiot
2009-05-13 10:54:51
I treid but it doesn't work same result,if any thing missing in IIS setting.thanks for the help.
Xyz
2009-05-13 11:09:06
thanks for the help.I tried this code but it saying "Request not authenticated" is i am missing any thing.y i am getting this message.please help
Xyz
2009-05-13 12:07:30
Got it!!You need to disable the anonymous access to the web application from IIS manager. Because if anon access is enabled and you don't need to enter user id/password and Request.IsAuthenticated is false as it is working with anonymous profile.
TheVillageIdiot
2009-05-14 04:20:53
thanks for the help,can u confirm one thing,Is windows authetication works when systems are under domain?
Xyz
2009-05-14 05:28:59
It works when PC is in domain and also on workgroup (or individual) systems.In domain you login is authenticated by domain controller and otherwise by the system from where you are accessing website.
TheVillageIdiot
2009-05-14 07:07:24
I tried in Work Group Systems,but not working same domain\username in all the systems..any idea
Xyz
2009-05-14 13:48:56