AK47
AK47

Reputation: 3807

Writing a text file to a mapped drive results in an error - "Could not find a part of the path"

  1. I have mapped network path (\fileserver\myfolder\dev) to "N:" drive using user "abc"
  2. Writing a file to drive using path "N:"
  3. My IIS is running under application pass through.

It's working in local but when I deploy it to DEV server it's not working there. It's throwing the error

"Could not find a part of the path"

Not sure what needs to be done?

Upvotes: 1

Views: 1852

Answers (1)

China Syndrome
China Syndrome

Reputation: 993

IIS runs under a different account and might not see the mapped drive. Try the UNC path //fileserver/myfolder/dev

if that does not work, if you have a username/password that can access the mapped path u can try impersonation

public static class ImpersonationContext
        {

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

        static WindowsImpersonationContext impersonationContext;

        [DllImport("advapi32.dll")]
        public 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)]
        public static extern int DuplicateToken(IntPtr hToken,
            int impersonationLevel,
            ref IntPtr hNewToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();

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


        public static bool ImpersonateUser(String userName, String domain, String password)
            {
            WindowsIdentity tempWindowsIdentity;
            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)
                        {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                        if (impersonationContext != null)
                            {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                            }
                        }
                    }
                }
            if (token != IntPtr.Zero)
                CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
            return false;
            }


        // MADE CHANGE HERE TO IMPERSONATION TO CHECK FOR NULL BEFORE PERFORMING ANY METHOD...WAS CAUSING PROBLEMS
        public static void UndoImpersonation()
            {
            if (impersonationContext != null)
                {

                impersonationContext.Undo();
                }
            }


        }

Upvotes: 1

Related Questions