System Username and Domain

  • Thread starter Thread starter 1qa2ws
  • Start date Start date
1

1qa2ws

Hi,

I need to get the windows current logged username and domain. Is it any
possibility?

1qa2ws
 
If you are using WinForms or ASP.NET with Windows authentication:

WinForms:

System.Security.Principal.WindowsIdentity wi =
System.Security.Principal.WindowsIdentity.GetCurrent();

ASP.NET:

System.Security.Principal.WindowsIdentity wi =
(System.Security.Principal.WindowsIdentity)HttpContext.Current.User;


HTH,
~d
 
How it differs from System.Environment.UserName and
System.Environment.UserDomainName?

1qa2sw
 
Well, because, in the case of using ASP.NET, System.Environment.UserName
/System.Environment.UserDomainName will return the name of the currently
logged on user on the server. Not the client using your web application.

In winforms, I suppose you could use System.Environment with no problem.

HTH,
~d
 
DotNet Coder said:
Well, because, in the case of using ASP.NET, System.Environment.UserName
/System.Environment.UserDomainName will return the name of the currently
logged on user on the server. Not the client using your web application.

In winforms, I suppose you could use System.Environment with no problem.

IMO, OP wasn't asking about ASP.NET, anyway your answer is incorrect. An
empty or a bogus value will be returned by calling
System.Environment.UserName from within asp.net, the reason is that "aspnet"
has no associated profile (and as such no Environment block) as it is not an
interactive login. Even if asp.net run's with domain credentials or as an
interactive login it's profile isn't loaded and the Environment shouldn't be
trusted.
Note that in general calling System.Environment is only a valid option when
the caller is associated with a login that has it's profile loaded (an
interactive login), in all other cases it shouldn't be used.

Willy.
 
Back
Top