Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ASP.NET中模拟时访问映射驱动器_Asp.net_Networking_Impersonation_Mapped Drive - Fatal编程技术网

在ASP.NET中模拟时访问映射驱动器

在ASP.NET中模拟时访问映射驱动器,asp.net,networking,impersonation,mapped-drive,Asp.net,Networking,Impersonation,Mapped Drive,简短版本:是否可以在ASP.NET中使用模拟来访问映射的驱动器? 长版本: 我目前正在ASP.NET中使用模拟来访问网络文件。这对于使用UNC路径的任何网络文件都非常有效,但它无法访问为我模拟的用户帐户定义的映射驱动器上的任何文件 例如,假设一个文件位于网络上的\\machine\folder\file.txt,还假设驱动器s:映射到\\machine\folder。我们需要能够访问完整的UNC路径\\machine\folder\file.txt,以及较短的映射驱动器路径S:\file.txt

简短版本:是否可以在ASP.NET中使用模拟来访问映射的驱动器?

长版本:

我目前正在ASP.NET中使用模拟来访问网络文件。这对于使用UNC路径的任何网络文件都非常有效,但它无法访问为我模拟的用户帐户定义的映射驱动器上的任何文件

例如,假设一个文件位于网络上的
\\machine\folder\file.txt
,还假设驱动器
s:
映射到
\\machine\folder
。我们需要能够访问完整的UNC路径
\\machine\folder\file.txt
,以及较短的映射驱动器路径
S:\file.txt

显然,标准ASP.NET进程也无法访问

使用在本地帐户下运行的带有映射的
S:
驱动器的控制台应用程序,调用
File.Exists(@“\\machine\folder\File.txt”)
返回true,而
File.Exists(@“S:\File.txt”)
也返回true

但是,当使用相同的本地帐户在ASP.NET上下文中模拟时,只有
File.Exists(@“\\machine\folder\File.txt”)
返回true
File.Exists(@“S:\File.txt”)
返回false

我正在本地Windows7 Professional box上使用IIS7进行测试,不过这需要同时在IIS6和IIS7中运行

模拟是用C#中的几个类处理的,我将在这里包括:

public static class Impersonation
{
    private static WindowsImpersonationContext context;

    public static void ImpersonateUser(string username, string password)
    {
        ImpersonateUser(".", username, password);
    }

    public static void ImpersonateUser(string domain, string username, string password)
    {
        StopImpersonating();

        IntPtr userToken;
        var returnValue = ImpersonationImports.LogonUser(username, domain, password,
                                                  ImpersonationImports.LOGON32_LOGON_INTERACTIVE,
                                                  ImpersonationImports.LOGON32_PROVIDER_DEFAULT,
                                                  out userToken);
        context = WindowsIdentity.Impersonate(userToken);
    }

    public static void StopImpersonating()
    {
        if (context != null)
        {
            context.Undo();
            context = null;
        }
    }
}

public static class ImpersonationImports
{
    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_LOGON_NETWORK = 3;
    public const int LOGON32_LOGON_BATCH = 4;
    public const int LOGON32_LOGON_SERVICE = 5;
    public const int LOGON32_LOGON_UNLOCK = 7;
    public const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
    public const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern int LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        out IntPtr phToken
        );
    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern int ImpersonateLoggedOnUser(
        IntPtr hToken
    );

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

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern int CloseHandle(IntPtr hObject);
}
然后,在页面加载过程中,我们基本上执行以下操作:

Impersonation.ImpersonateUser("DOMAIN", "username", "password");

if (!File.Exists(@"S:\file.txt"))
     throw new WeCannotContinueException();

我意识到使用映射驱动器不是最佳做法,但出于传统原因,这对我们的业务是可取的是否可以在ASP.NET中使用模拟来访问映射的驱动器?

否,但可以使用符号链接
Mklink/d
将创建目录链接。

您只能访问被模拟用户创建的映射驱动器

因此,如果您要模拟用户X,然后映射共享(例如,通过网络使用),那么只要模拟有效,该共享将一直可见

您可以通过
DriveInfo.GetDrives()
确定当前可访问的映射驱动器。在当前安全上下文中可以访问具有网络驱动器类型的驱动器。

我尝试了,但它不能与ASP.NET一起使用

至于阿恩谢的回答:根本不起作用;我甚至与域管理员进行了模拟,并将所有权限设置为everyone、iuser和network service

所以,唯一的解决方案是:在设计web应用程序时,必须决定是否要保存到网络资源,并为此使用UNC协议


映射的网络驱动器不能与ASP.NET一起用于常规文件操作。

谢谢。mklink的唯一帮助是,如果我们可以继续使用映射的驱动器路径(即“S:\file.txt”)。但除非我弄错了,否则我不相信mklink允许这样做。而且,对于我自己和其他感兴趣的人,你能详细说明为什么这不可能吗?我认为这是可行的。我们有一个(原生C++)服务,它作为LocalSystem运行,并在必要时模拟用户;它可以很好地访问映射驱动器。我刚刚在Windows7上进行了验证,以进行双重检查。我不知道为什么这在ASP.NET下不起作用。谢谢,这对我来说很有效,但我不关心映射网络驱动器。(我的客户[认为他们]知道,但他们不知道
Mklink/d
。客户==>受过教育。问题==>解决了)嘿,亚当。。您能够使用asp.net访问映射驱动器吗?