views:

374

answers:

2

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
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
I treid but it doesn't work same result,if any thing missing in IIS setting.thanks for the help.
Xyz
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
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
thanks for the help,can u confirm one thing,Is windows authetication works when systems are under domain?
Xyz
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
I tried in Work Group Systems,but not working same domain\username in all the systems..any idea
Xyz