Simsons
Simsons

Reputation: 12745

How to Get the currently logged on user name

I am building a small intranet app in which I need to display the user name of the person who opens the webpage.

I tries with:

WindowsIdentity.GetCurrent().Name.ToString();

But this always displays my user name on the webpage.

Upvotes: 2

Views: 17067

Answers (4)

Sergey K
Sergey K

Reputation: 4114

use this code

if (User.Identity.IsAuthenticated)
 Label1.Text = User.Identity.Name;
else
 Label1.Text = "No user identity available.";

look at the following article Forms Authentication in ASP.NET

Upvotes: 0

Christoph Fink
Christoph Fink

Reputation: 23113

Please see THIS - basicaly you need to enable Integrated windows authentication and the use one of the following:

System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;    
string strName = p.Identity.Name;

or

string strName = HttpContext.Current.User.Identity.Name.ToString();

or

string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name    
string strName = Request.ServerVariables[5]; //Finding with index

All 3 cases should retrun a string containing DomainName\WinNTLoggedUserName.

Upvotes: 5

jao
jao

Reputation: 18620

See http://forums.asp.net/t/1102996.aspx/1. I think the reason that you see your own name is that the web app is running under your credentials.

So use HttpContext.Current.User.Identity.Name instead and make sure that you have enabled Forms authentication in web.config

Upvotes: 0

Ken D
Ken D

Reputation: 5968

You can use this:

Page.User.Identity.Name

P.S: Don't forget to check entities whether they were null first.

Upvotes: 0

Related Questions