Question

I have to create many files in Network drive with specified user.

I used this answer to connect different user I use Impersonator Class :

public class Impersonator : IDisposable
{

    const int LOGON32_PROVIDER_DEFAULT = 0;
    const int LOGON32_LOGON_INTERACTIVE = 2;

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public extern static bool CloseHandle(IntPtr handle);

    private IntPtr token = IntPtr.Zero;
    private WindowsImpersonationContext impersonated;
    private readonly string _ErrMsg = "";

    public bool IsImpersonating
    {
        get { return (token != IntPtr.Zero) && (impersonated != null); }
    }

    public string ErrMsg
    {
        get { return _ErrMsg; }
    }

    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
    public Impersonator(string userName, string password, string domain)
    {
        StopImpersonating();

        bool loggedOn = LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token);
        if (!loggedOn)
        {
            _ErrMsg = new System.ComponentModel.Win32Exception().Message;
            return;
        }

        WindowsIdentity identity = new WindowsIdentity(token);
        impersonated = identity.Impersonate();
    }

    private void StopImpersonating()
    {
        if (impersonated != null)
        {
            impersonated.Undo();
            impersonated = null;
        }

        if (token != IntPtr.Zero)
        {
            CloseHandle(token);
            token = IntPtr.Zero;
        }
    }

    public void Dispose()
    {
        StopImpersonating();
    }
}

and the code :

using (Impersonator impersonator = new Impersonator("UserName", "UserPwd", "UserDomaine"))
{
    if (!Directory.Exists("Z:\\")) // check if Network drive exist
    {
       NetworkDrive drive = new NetworkDrive
       {
           ShareName = @"\\IP\Partage",
           LocalDrive = "Z",
           Force = true
       };

       drive.MapDrive(@"UserDomaine\UserName", "UserPwd");

     }
     File.Create(@"Z:\Log\FileName.txt");
}

But in this case I found that the code Map the drive every time that I have to create a file or update it!! And I have a lot of work with this function.

There’s a solution to not map it every time?

I tried to Map the driver in with this user in opening of application but same problem.

Was it helpful?

Solution

I think you don't need to map the drive. After impersonating you can just create the file directly using the network drive and it will create the file as impersonated user.

using (Impersonator impersonator = new Impersonator("UserName", "UserPwd", "UserDomaine"))
{
    File.Create(@"\\IP\Partage\Log\FileName.txt");
}

OTHER TIPS

Try not to use Using block. declare Impersonator as global static variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top