Answered by:
Impersonation as domain user still can’t get access to a network drive

Question
-
Hello
I found some threads regarding this issue but still unable to resolve this problem.
I’m using ImpersonationContext class for the impersonation :
/// <summary>
/// Leverages the Windows API (advapi32.dll) to programmatically impersonate a user.
/// </summary>
public class ImpersonationContext : IDisposable
{
#region constants
private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_PROVIDER_DEFAULT = 0;
#endregion
#region global variables
private WindowsImpersonationContext impersonationContext;
private bool impersonating;
#endregion
#region unmanaged code
[DllImport("advapi32.dll")]
private static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool CloseHandle(IntPtr handle);
#endregion
#region constructors
public ImpersonationContext()
{
impersonating = false;
}
/// <summary>
/// Overloaded constructor and begins impersonating.
/// </summary>
public ImpersonationContext(string userName, string password, string domain)
{
this.BeginImpersonationContext(userName, password, domain);
}
#endregion
#region impersonation methods
/// <summary>
/// Begins the impersonation context for the specified user.
/// </summary>
/// <remarks>Don't call this method if you used the overloaded constructor.</remarks>
public void BeginImpersonationContext(string userName, string password, string domain)
{
//initialize token and duplicate variables
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
using (WindowsIdentity tempWindowsIdentity = new WindowsIdentity(tokenDuplicate))
{
//begin the impersonation context and mark impersonating true
impersonationContext = tempWindowsIdentity.Impersonate();
impersonating = true;
}
}
}
}
//close the handle to the account token
if (token != IntPtr.Zero)
CloseHandle(token);
//close the handle to the duplicated account token
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
}
/// <summary>
/// Ends the current impersonation context.
/// </summary>
public void EndImpersonationContext()
{
//if the context exists undo it and dispose of the object
if (impersonationContext != null)
{
//end the impersonation context and dispose of the object
impersonationContext.Undo();
impersonationContext.Dispose();
}
//mark the impersonation flag false
impersonating = false;
}
#endregion
#region properties
/// <summary>
/// Gets a value indicating whether the impersonation is currently active.
/// </summary>
public bool Impersonating
{
get
{
return impersonating;
}
}
#endregion
#region IDisposable implementation
~ImpersonationContext()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (impersonationContext != null)
{
impersonationContext.Undo();
impersonationContext.Dispose();
}
}
}
#endregion
}
I’m running VS 2012 as different user – with domain user which isn’t in the administrator group
In the code below I impersonate as admin user which has all the access privileges
using (ImpersonationContext context = new ImpersonationContext("Admin", "pass", "domain"))
{
if (context.Impersonating)
{
string NetworkDrive = "Q: \\Images\\IMG_8381.JPG";
string localDrive = "D:\\Images\\767LAN.jpg";
if (File.Exists(NetworkDrive))
{
// never find the file
}
if (File.Exists(localDrive))
{
// find file }
}
}
}
networkDrive always return not Exists
thanks
Sunday, April 21, 2013 2:07 PM
Answers
-
I do not believe that impersonation actually loads the user profile, which is where the mapped drives show. This means that if Q is a mapped network drive, it is not known. You should try using the actual network address that Q maps to, rather than Q.
Mike
- Proposed as answer by Patrice ScribeMVP Sunday, April 21, 2013 4:32 PM
- Marked as answer by roywax Monday, April 22, 2013 7:56 AM
Sunday, April 21, 2013 3:29 PM -
Hi,
Would second this. Try to see what happens with a UNC path (such as \\servername\sharename\dir\file.txt) rather than a drive letter that depends on the user profile...
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
- Marked as answer by roywax Monday, April 22, 2013 7:57 AM
Sunday, April 21, 2013 4:35 PM
All replies
-
I do not believe that impersonation actually loads the user profile, which is where the mapped drives show. This means that if Q is a mapped network drive, it is not known. You should try using the actual network address that Q maps to, rather than Q.
Mike
- Proposed as answer by Patrice ScribeMVP Sunday, April 21, 2013 4:32 PM
- Marked as answer by roywax Monday, April 22, 2013 7:56 AM
Sunday, April 21, 2013 3:29 PM -
Hi,
Would second this. Try to see what happens with a UNC path (such as \\servername\sharename\dir\file.txt) rather than a drive letter that depends on the user profile...
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
- Marked as answer by roywax Monday, April 22, 2013 7:57 AM
Sunday, April 21, 2013 4:35 PM