【问题标题】:Writing a text file to a mapped drive results in an error - "Could not find a part of the path"将文本文件写入映射驱动器会导致错误 - “找不到路径的一部分”
【发布时间】:2018-09-07 17:57:26
【问题描述】:
  1. 我已使用用户“abc”将网络路径 (\fileserver\myfolder\dev) 映射到“N:”驱动器
  2. 使用路径“N:”将文件写入驱动器
  3. 我的 IIS 在应用程序直通下运行。

它在本地工作,但是当我将它部署到 DEV 服务器时,它在那里不工作。它抛出错误

“找不到路径的一部分”

不确定需要做什么?

【问题讨论】:

  • 该位置是否映射到服务器上的驱动器“N”? IIS 应用程序池是否有权访问该目录?
  • 位置映射到服务器上的“N”驱动器。不确定如何通过 IIS 授予访问权限
  • 您在登录到服务器时右键单击文件夹(目录),运行您的应用程序的应用程序池在您创建它时有一个名称,然后当您进入属性选项的安全性时右键单击目录,添加用户,然后使用(确保 IIS AppPool\AppPoolName 中有空格)IIS AppPool\AppPoolName,在整个计算机上搜索(服务器名称),而不是使用 domain\something,然后将其定位为一个有效的用户,设置你认为合适的安全选项。
  • @RyanWilson - 谢谢!我现在,我在 AppPool 的身份中输入了用户凭据,该用户有权访问共享路径并在我的代码中使用了 UNC 路径。这现在对我有用。不确定这是否是最好的方法。感谢您的帮助。
  • 不客气。如果您用于运行应用程序池的域帐户是安全的(强密码)我认为您的方法没有任何问题,我有几个应用程序在专用域帐户下运行,以便应用程序可以执行发送等操作将文档发送到网络打印机和另一个使用 HttpClient 调用使用 Windows 身份验证的 API。

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


【解决方案1】:

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


        }

【讨论】:

  • UNC 路径不起作用。这个 Impersonate 可以与 .NET CORE 一起使用吗?
  • 这是我在 .net 4.5 中使用的代码,如果您的核心应用程序在 Windows 服务器上,那么是的,
猜你喜欢
  • 2012-04-28
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
相关资源
最近更新 更多