Hi all,

I am writing one application using c# in which firstly I m getting impersonating the user and then i want to retrieve mapped Drives.

But issue is when I am calling method in following sequence:
1. create object of class
2. call mappeddrives() method
3. call impersonate method

then its working fine.
But if i m calling impersonate method before calling mappeddrives() then its not giving me list of mapped drives.

my code for getting mapped drives is :

public string MappedDrives()
        {
            string drives = "";
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            foreach (DriveInfo dirInfo in allDrives)
            {
                if (dirInfo.DriveType.ToString() == "Network")
                    drives += dirInfo.Name+"?";                           
            }
           return drives;
        }

Can anyone tell me the reason behind this?

Thanks,
Anu

Dani AI

Generated

A few clarifying points that build on and and explain why saw that behavior.

Mapped drive letters are per-interactive-logon-session and tied to the user's profile (persistent mappings are recorded under HKCU\Network). Simply impersonating a thread (swapping the security token) does not automatically attach the process to the original interactive session or load that user’s HKCU, so APIs like DriveInfo.GetDrives will only show the drives visible to the current process/session. That is also why IIS worker processes (which run in a service session and normally do not load interactive profiles) will not see Explorer-created mappings regardless of impersonation or admin membership.

Practical approaches:

  • Prefer UNC paths (\server\share) and explicit credentials instead of relying on drive letters. That avoids session/visibility issues.
  • Programmatically create the connection when needed (for example via WNetAddConnection2 with credentials) so the process has an authenticated network handle it can use.
  • If enumerating the actual mapped letters for a specific user is required, create a real logon session for that user (LogonUser with an interactive logon) and call LoadUserProfile or run the code in the user’s session (WTSQueryUserToken/CreateProcessAsUser). Those steps load HKCU and give access to that session’s mappings.
  • For elevation-linked visibility (different problem but often confused), the EnableLinkedConnections registry tweak lets elevated processes see mapped drives created in the standard user session (requires admin and reboot) — use with caution.

Quick checklist to try: log in interactively as the target user to confirm the mapping exists; for a console app use LogonUser+LoadUserProfile before enumerating; for IIS use UNC paths or programmatic connections or run the app pool under a dedicated domain account. Loading other users’ profiles and creating logon sessions have security implications, so prefer UNC + service account where possible.

Recommended Answers

All 3 Replies

are you sure the user that you are impersonating has permissions to the mapped drives?

try logging in as that user and seeing if the mapped drives exist

impersonating user is member of administartors group.

And also when i tried deploying that application in IIS, its not giving dmapped drives in either case(means order of calling function does not matter in case of IIS)

Thanks
Anu

Um, if its not running directly in the users session then it wont have any mapped drives - as its not the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.