#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Mumbai
    Posts
    1
    Rep Power
    0

    How to get NT logon user id in C#?


    Hello,

    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:

    1) System.Environment.UserName
    Returns "ASPNET"

    2) User.Identity.Name
    Returns "" (empty)

    3) System.Security.Principal.WindowsIdentity.GetCurrent().Name
    Returns "MACHINE-NAME\ASPNET"

    Could someone please help me out in this?

    medhavi
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    Hmm, whats wrong with your output? The...
    NT user ID (the currently logged in user) from the codebehind
    ...*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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    1
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    2
    Rep Power
    0

    Exclamation 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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Smile


    easy peasy:

    Code:
    WindowsIdentity.GetCurrent().Name
    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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    13
    Rep Power
    0
    hi gamin

    Here is a best solution for You

    using System.Security.Principal;

    string sUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

    MessageBox.Show(sUserName);

IMN logo majestic logo threadwatch logo seochat tools logo