Avinash patil
Avinash patil

Reputation: 1799

Copying file to shared drive fails in ASP.net c#

I have ASP.Net and C# application. I am uploading images to the site and store them in the C:\Images directory, which works fine. When I save images to the C:\Images folder and simultaneously copy (or some times move) to the shared drive, I use the shared drive physical address, which looks like \\192.xxx.x.xx\some folder\Images. This drive is mapped to the deployment server. I am using IIS hosting for the site.

The problem is with the shared drive copying. When I use the site from local machine (where the site is deployed) that copies the file to the shared drive. But when I use the site from another machine (other than the deployed server) that saves the image in C:\Images, but it won't copy the file to the shared drive.

Here's the code I'm using

**Loggedon method shows success in debug.

  public static void CopytoNetwork(String Filename)
    {
        try
        {

            string updir = System.Configuration.ConfigurationManager.AppSettings["PhysicalPath"].ToString();

            WindowsImpersonationContext impersonationContext = null;
            IntPtr userHandle = IntPtr.Zero;
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;
            String UserName = System.Configuration.ConfigurationManager.AppSettings["Server_UserName"].ToString();
            String Password = System.Configuration.ConfigurationManager.AppSettings["server_Password"].ToString();
            String DomainName = System.Configuration.ConfigurationManager.AppSettings["Server_Domain"].ToString();


            bool loggedOn = LogonUser(UserName, DomainName, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref userHandle); 
            try
            {
                File.Move(@"C:\Images\" + Filename, updir + "\\" + Filename);
             }
            catch (Exception)
            {
            }
            finally
            {
                if (impersonationContext != null)
                {
                    impersonationContext.Undo();
                }

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

        }
        catch (Exception)
        {
        }
    }

Upvotes: 2

Views: 2095

Answers (1)

N. Jensen
N. Jensen

Reputation: 54

You can set up an impersonated user class like this:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;

public class ImpersonatedUser : IDisposable
{
    IntPtr userHandle;
    WindowsImpersonationContext impersonationContext;

    public ImpersonatedUser(string user, string domain, string password)
    {
        userHandle = IntPtr.Zero;
        bool loggedOn = LogonUser(
            user,
            domain,
            password,
            LogonType.Interactive,
            LogonProvider.Default,
            out userHandle);

        if (!loggedOn)
            throw new Win32Exception(Marshal.GetLastWin32Error());

        // Begin impersonating the user
        impersonationContext = WindowsIdentity.Impersonate(userHandle);
    }

    public void Dispose()
    {
        if (userHandle != IntPtr.Zero)
        {
            CloseHandle(userHandle);
            userHandle = IntPtr.Zero;
            impersonationContext.Undo();
        }
    } 

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        LogonType dwLogonType,
        LogonProvider dwLogonProvider,
        out IntPtr phToken
        );

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool CloseHandle(IntPtr hHandle);

    enum LogonType : int
    {
        Interactive = 2,
        Network = 3,
        Batch = 4,
        Service = 5,
        NetworkCleartext = 8,
        NewCredentials = 9,
    }

    enum LogonProvider : int
    {
        Default = 0,
    }
}

When you need to do the file copying you do it like this:

    using (new ImpersonatedUser(<UserName>, <UserDomainName>, <UserPassword>))
    {
        DoYourFileCopyLogic();
    }

Upvotes: 1

Related Questions