简体   繁体   English

来自共享驱动器的 C# System.IO.File.Copy 在本地主机上工作,但在服务器上拒绝访问该路径

[英]C# System.IO.File.Copy from shared drive works on localhost but access to the path is denied on server

I want to copy a pdf from a shared folder to a project folder.我想将 pdf 从共享文件夹复制到项目文件夹。 So I use System.IO.File.Copy:所以我使用 System.IO.File.Copy:

System.IO.File.Copy(@"\\netstore\IT\SIGN\112454.pdf", "C:\inetpub\wwwroot\myaspmvcwebapisite\temp\112454.pdf", true);

The file copies when I run the application locally on my machine but when I publish it to the server and go to the site I get:当我在我的机器上本地运行应用程序时,文件会复制,但是当我将它发布到服务器并转到该站点时,我得到:

signature-service.ts:120 {"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Access to the path '\\\\netstore\\IT\\SIGN\\112454.pdf' is denied.","ExceptionType":"System.UnauthorizedAccessException","StackTrace":"   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)\r\n   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)\r\n   at api.valbruna.local.Controllers.ValShip.SignatureController.Post(Int32 id)"}

How do I grant access to the site to access that file location?如何授予对站点的访问权限以访问该文件位置?

Please note:请注意:

  • The site is a MVC Web API INTRANET application该站点是一个MVC Web API INTRANET 应用程序
  • It uses windows based authentication它使用基于 Windows 的身份验证
  • When I say localhost I mean within visual studio(IISExpress)当我说 localhost 我的意思是在 Visual Studio(IISExpress)

What I have tried:我尝试过的:

  • Granting EVERYONE access to the folder \\\\netstore\\IT\\SIGN\\授予每个人对文件夹 \\\\netstore\\IT\\SIGN\\ 的访问权限

I have looked at other posts( Access to the path is denied ) on stack overflow but most of the answers say to grant access to IIS_USRS and I have given everyone access so that doesn't work.我在堆栈溢出时查看了其他帖子( 访问路径被拒绝),但大多数答案都说授予对 IIS_USRS 的访问权限,并且我已授予每个人访问权限,因此这不起作用。

---Update 1--- ---更新1---

@Chris Pratt I have updated my IIS settings based on the link you provided. @Chris Pratt 我已经根据您提供的链接更新了我的 IIS 设置。

  1. The identity field has been updated from NetworkService to ApplicationPoolIdentity.身份字段已从 NetworkService 更新为 ApplicationPoolIdentity。

在此处输入图片说明

  1. I granted full access to the <domainname>\\<machinename>$我授予了对<domainname>\\<machinename>$完全访问权限

在此处输入图片说明

---Update 2--- ---更新2---

@jp2code I turned on network discovery and I am still getting the same error. @jp2code 我打开了网络发现,但仍然遇到同样的错误。

在此处输入图片说明

---Update 3--- ---更新3---

As a temporary workaround using an Impersonation class works in accessing the shared folder:作为使用Impersonation 类的临时解决方法可用于访问共享文件夹:

using (new Impersonator("username", System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName, "password"))
{
    // code that executes under the new context.
    sizingDoc.LoadFromFile(sigDocUrl);
}

Are you saying it runs fine when hosted in a local IIS or within Visual Studio (IIS Express)?您是说它在本地 IIS 或 Visual Studio (IIS Express) 中托管时运行良好? IIS Express runs under the active user account, but IIS runs under the App Pool's Identity. IIS Express 在活动用户帐户下运行,但 IIS 在应用程序池的标识下运行。 Granting access to IIS_USRS is enough for a local directory, but a remote directory will require authentication, which means it will authenticate via the App Pool's Identity.授予对 IIS_USRS 的访问权限对于本地目录就足够了,但远程目录需要身份验证,这意味着它将通过应用程序池的身份进行身份验证。 This article from Microsoft may give you some extra pointers in getting this up and running. 来自 Microsoft 的这篇文章可能会为您提供一些额外的指导来启动和运行它。

However, without customizing anything, the fact that the App Pool runs as a network service, should be enough to allow you grant access.但是,无需自定义任何内容,应用程序池作为网络服务运行的事实应该足以允许您授予访问权限。 Network services are authorized via the machine account ( <domainname>\\<machinename>$ ).网络服务通过机器帐户 ( <domainname>\\<machinename>$ ) 授权。 So, if you authorize the network share for that account, you should be good.因此,如果您为该帐户授权网络共享,则应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM