Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:29:06+00:00 2026-05-25T21:29:06+00:00

I am using something like the following method to impersonate a user in my

  • 0

I am using something like the following method to impersonate a user in my code:

How do you do Impersonation in .NET?

In another class, I need to find out the current user (like “mydomain\moose”), but I won’t have any idea if I’m currently impersonating another user or not.

How do I get the username, if I’m impersonating someone?

System.Environment.UserName and System.Security.Principal.WindowsIdentity.GetCurrent().Name both return the original user, not the currently impersonated user.

More Details:

I am doing this impersonation so that I can access some files in a netowrk share the user usually does not have access to.

If I use a logon type of LOGON32_LOGON_INTERACTIVE, I do see the new user, but I cannot access the network share. If I use a logon type of LOGON32_LOGON_NEW_CREDENTIALS (a value of 9), i can access the network share but I don’t see the new user in Environment.UserName.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:29:07+00:00Added an answer on May 25, 2026 at 9:29 pm

    First, I’d like to point out what the property WindowsIdentity.GetCurrent().Name will return
    if you use LOGON32_LOGON_NEW_CREDENTIALS or LOGON32_LOGON_INTERACTIVE as logon type for the LogonUser (inside the impersonation class) function:

    1. Using LOGON32_LOGON_INTERACTIVE

      // Assuming this code runs under USER_B
      
      using (var imp = new Impersonation("treyresearch", "USER_A", "SecurePwd", LOGON32_LOGON_INTERACTIVE ))
      {
        // Now, we run under USER_A
        Console.Out.WriteLine(WindowsIdentity.GetCurrent().Name); // Will return USER_A
      }
      
    2. Using LOGON32_LOGON_NEW_CREDENTIALS

      // Assuming this codes runs under USER_B
      
      using (var imp = new Impersonation("treyresearch", "USER_A", "SecurePwd", LOGON32_LOGON_NEW_CREDENTIALS ))
      {
        Console.Out.WriteLine(WindowsIdentity.GetCurrent().Name); // Will return USER_B
      }
      

    This is the behaviour as you have described in your question and is consistent with the description on MSDN for the LogonUser function. For LOGON32_LOGON_NEW_CREDENTIALS the created user token is just a clone of the current user token. This means that the created user session has the same identifier as the calling thread. The passed credentials to the LogonUser function are only used for outbound network connections.

    Second, let me point out two situation where the described difference between LOGON32_LOGON_INTERACTIVE and LOGON32_LOGON_NEW_CREDENTIALS becomes clear:

    • Two domain joined computers: computer_A, computer_B
    • Two users: user_A (local admin on computer_A), user_B (only standard user rights on B)
    • One networkshare on computer_B (mynetworkshare, user_B does have permission to access share).
    • One local folder on computer_A (only user_A has permission to write to this folder).

    You run your program on computer_A (under the account of user_A). You impersonate user_B (using LOGON32_LOGON_INTERACTIVE). Then you connect to the network share on computer_B and try to copy a file to the local folder (only user_A has the permission to write to this folder). Then, you get an access denied error message, because the file operation is done with the permissions of user_B who does not have permission on the local folder.

    Same situation as above. But now, we use LOGON32_LOGON_NEW_CREDENTIALS to impersonate user_B. We connect to the network drive and copy a file from the network drive to the local folder. In this case the operation succeeds because the file operation is done with the permissions of user_A.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using .NET Regular Expressions to strip HTML code. Using something like: <title>(?<Title>[\w\W]+?)</title>[\w\W]+?<div class=article>(?<Text>[\w\W]+?)</div>
I need to build an array using something like the following: CLLocationCoordinate2D points[4]; points[0]
I am using something like the above sample code but when i try to
Im using a thirdparty class that has the following method: public void print(PrintStream s);
Using JavaMail I would do something like the following: ... Address address[] = ...;
So I have a helper method that looks something like the following: private D
I really need to create something like the following, I am building 2 classes,
I've got a library defined by another team. It has something like the following:
When writing async method implementations using the BeginInvoke/EndInvoke pattern the code might look something
Using something like this: try: # Something... except Exception as excep: logger = logging.getLogger(component)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.