简体   繁体   中英

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. So I use 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
  • It uses windows based authentication
  • When I say localhost I mean within visual studio(IISExpress)

What I have tried:

  • Granting EVERYONE access to the folder \\\\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.

---Update 1---

@Chris Pratt I have updated my IIS settings based on the link you provided.

  1. The identity field has been updated from NetworkService to ApplicationPoolIdentity.

在此处输入图片说明

  1. I granted full access to the <domainname>\\<machinename>$

在此处输入图片说明

---Update 2---

@jp2code I turned on network discovery and I am still getting the same error.

在此处输入图片说明

---Update 3---

As a temporary workaround using an Impersonation class works in accessing the shared folder:

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 Express runs under the active user account, but IIS runs under the App Pool's Identity. 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. This article from Microsoft may give you some extra pointers in getting this up and running.

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>$ ). So, if you authorize the network share for that account, you should be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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