September 16th, 2003, 02:56 AM
How to get NT logon user id in C#?
It's been a while since I started programming in C#. Primarily I'd been working on ASP and things were easy to obtain though the method was clumsy.
However, I've been facing some problems to get the NT user ID (the currently logged in user) from the codebehind.
ASP.NET service creates an account viz. "ASPNET" (a worker account) to access the services and this account requires some privilages to be able to access the .NET service.
When I attempt to retrieve the current logged in user's id, it always returns me "MACHINE-NAME\ASPNET", as this is the account used to access the ASP.NET service.
I've tried several ways:
Returns "" (empty)
Could someone please help me out in this?
September 16th, 2003, 12:25 PM
Hmm, whats wrong with your output? The...
...*is* MACHINE-NAME\ASPNET (as long as you don't change your IIS settings (each web app can run as a separate user))
IIS always runs without any *real* user context, it will always use a special account unless otherwise specified.
October 3rd, 2003, 10:02 AM
If you're just trying to access this information from a web page you may be able to use the following
If the web application is set to use windows authentication, then you may just need to add the following line of code to your web.config file.
<identity impersonate="true" />
By default, ASP.NET applications do not impersonate. The applications run using the configured ASP.NET process identity (usually ASPNET) and all resource access performed by your application uses this identity.
When you enable impersonation you should be able to see the actual user ID when you use the code that you have in your original post
June 22nd, 2004, 06:38 PM
You must configure IIS also
One thing that I struggled with was the fact that IIS has to be configured to not allow anonymous connections for the directory that contains the page(s) in which you would like to get the username. The following article got me going in the right direction: http://msdn.microsoft.com/library/de...SecNetAP05.asp.
June 20th, 2013, 10:18 AM
If you want a different user under ASP.Net you need to exit the Web.Config and enable Impersonation. Then this call will return the impersonated acct.
June 27th, 2013, 02:21 AM
Here is a best solution for You
string sUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;