将文本文件写入C#中的映射驱动器-找不到路径的一部分

时间:2018-09-07 17:57:27

标签: c# .net iis core mapped-drive

  1. 我已使用用户“ abc”将网络路径(\ fileserver \ myfolder \ dev)映射到“ N:”驱动器。
  2. 使用路径“ N:”写入文件以进行驱动
  3. 我的IIS在应用程序直通下运行。

它在本地运行,但是当我将其部署到DEV服务器时,它不在本地运行。引发错误为“找不到路径的一部分”

不确定需要做什么?

1 个答案:

答案 0 :(得分:0)

IIS在另一个帐户下运行,并且可能看不到映射的驱动器。 尝试UNC路径// fileserver / myfolder / dev

如果这不起作用,如果您拥有可以访问映射路径的用户名/密码,则可以尝试模拟

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();
                }
            }


        }