关于asp.net:从IIS中部署的应用程序访问共享文件夹

Access shared folder from application deployed in IIS

当我尝试从本地网络中的共享文件夹访问文件时出现问题。
当我在Visual Studio中运行该应用程序时,它运行良好,可以下载该文件,但是当我将该应用程序部署到IIS时,它根本不起作用。
我不明白为什么,原因是我向所有用户(包括NETWORK SERVICE和IIS_USR)授予了完全权限。
我目前正在使用计算机进行测试。 它具有Windows 7并运行IIS 7.5。 我尝试访问的共享文件夹位于Windows Vista安装中,但未安装IIS。

下载文件的代码是这样的:

1
2
3
4
5
6
7
8
protected void button_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType ="application/octet-stream";
    Response.AddHeader("Content-Disposition","attachment; filename=S10.png");
    Response.WriteFile(@"\\\\192.168.1.82\\Machine\\file.png");
    Response.End();
}

它给了我以下错误:

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Acesso negado
ao caminho '\\192.168.1.82\\Machine\\file.png'.

ASP.NET is not authorized to access the requested resource. Consider
granting access rights to the resource to the ASP.NET request
identity. ASP.NET has a base process identity (typically
{MACHINE}\\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and
the configured application pool identity on IIS 7.5) that is used if
the application is not impersonating. If the application is
impersonating via , the identity will be
the anonymous user (typically IUSR_MACHINENAME) or the authenticated
request user.

To grant ASP.NET access to a file, right-click the file in Explorer,
choose"Properties" and select the Security tab. Click"Add" to add
the appropriate user or group. Highlight the ASP.NET account, and
check the boxes for the desired access.

请有人可以帮助我。
提前致谢。


您已经授予了文件系统的访问权限(对此,完全控制是矫kill过正,读取或修改应该没问题),但是您可能会发现共享权限仍处于默认的只读状态。您需要更改共享权限。

阅读此以获得更多信息。

编辑:调试时可以正常运行的原因是因为它使用的是您登录时使用的帐户,而不是VS中的aspnet帐户。

创建一个新的应用程序池,该服务池在服务帐户下运行,该服务帐户是IIS服务器上IIS_WPG组的成员,并且对要写回的共享具有权限(请记住将服务帐户设置为password永不过期)。授予IIS_WPG组"读取和执行",列出文件夹内容以及对网站目录的"读取"权限。更改Web应用程序以使其在新的Web池中运行。


如果文件和计算机文件夹位于C驱动器中,则还需要授予包含此文件路径的根文件夹权限,因为C驱动器中通常包含系统文件,因此通常受到读/写保护。

因此,例如,如果文件位于路径C:\ Machine \ file.png中。
然后,您需要向IIS用户组添加权限,并将其授予IIS用户组的"计算机文件夹",而不仅仅是文件(即file.png)。


解决方案1:

您可以定义域用户。让IIS服务器与此用户一起运行(通过池标识或"连接为")。授予该用户对"共享"文件夹的访问权限。

解决方案2:

将IIS服务器帐户添加到包含共享文件夹的服务器的用户组中,授予此用户对共享文件夹的访问权限。