Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.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/1/asp.net/32.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
C# 在asp.net中访问远程计算机需要建议_C#_Asp.net - Fatal编程技术网

C# 在asp.net中访问远程计算机需要建议

C# 在asp.net中访问远程计算机需要建议,c#,asp.net,C#,Asp.net,我想访问远程计算机上的某些位置。我想访问的文件夹对每个人都有完全控制权。下面给出的代码用于访问网络路径 System.IO.DirectoryInfo locationInfo = new System.IO.DirectoryInfo(BackupLocationTxt.Text); if (locationInfo.Exists) { // do some operations } 如果要访问的主机和远程计算机

我想访问远程计算机上的某些位置。我想访问的文件夹对每个人都有完全控制权。下面给出的代码用于访问网络路径

 System.IO.DirectoryInfo locationInfo = new System.IO.DirectoryInfo(BackupLocationTxt.Text);
        if (locationInfo.Exists)  
        {
            // do some operations
         }
如果要访问的主机和远程计算机都使用操作系统windows xp,则应用程序可以正常运行。如果应用程序在visual studio中运行,则应用程序也可以正常运行

然后我的问题是,任何一台机器(服务器和远程机器)的操作系统都比windows xp(如windows 7,server 2008)更新locationInfo.Exists始终为false

但是,如果应用程序在VisualStudio内部运行,那么它可以独立于操作系统正常工作

我在网上搜索了很多。但是还没有找到确切的解决办法。有人建议模仿。但我不知道该怎么做。模拟是解决我问题的方法吗??还是有更好的主意


任何帮助都将不胜感激

请尝试
System.IO.Directory.Exists


请记住,如果您没有对该目录的最低只读权限,Exists方法将返回false,如果您遇到了一个有趣的问题,则返回Null。您如何配置站点目录安全性?如果启用了用户的匿名访问,则打开给所有人的文件夹可能不允许访问,具体取决于服务器的操作系统(有关详细信息,请参阅t)

如果站点以匿名方式运行,则可以更改站点在IIS管理器中运行的帐户,或者启用模拟。当您在VisualStudio中运行站点时,该站点是使用您的权限运行的,因此匿名也不是问题

您可以使用以下代码输出您的站点正在运行的用户的身份,以帮助了解发生了什么。您可以在不进行任何模拟的情况下,向用户提供您的站点运行方式,以访问网络位置。将ASP:标签添加到您的页面并查看您的运行身份:

lblSomeLabel.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name
模拟可能会给您带来额外的安全风险,因此在进行更改之前,您应该进行更多的阅读-但是,您用于模拟的用户不需要是域管理员。在您的情况下,用户可能只需要拥有对网络位置的完全访问权限

您可以阅读有关如何启用模拟的更多信息。下面是我推荐的该页面中的一些代码。下面的代码不是让整个站点在模拟模式下运行,而是只运行您遇到问题的部分

public void Page_Load(Object s, EventArgs e)
{
    if(impersonateValidUser("username", "domain", "password"))
    {
        //Insert your code that runs under the security context of a specific user here.
        undoImpersonation();
    }
    else
    {
        //Your impersonation failed. Therefore, include a fail-safe mechanism here.
    }
}

private bool impersonateValidUser(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;
}

private void undoImpersonation()
{
    impersonationContext.Undo();
}

另外,在搜索安全文章时,我发现这值得一读。

还没有回复:)我的问题有什么问题吗?或者需要更多的解释吗?非常感谢。我试过模仿。你能看看这个问题吗