RWL01
RWL01

Reputation: 478

ASP.NET application gets system.unauthorizedaccessexception when accessing a network drive

I'm attempting to access a network drive from an ASP.NET page. Technically it's a SharePoint application page (_layouts/mycompany/mypage.aspx) but I believe the principles are the same.

string filePath = @"\\companyshare\temp\test.txt";
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

When I hit the second line in the code above I get the error:

Access to the path '\companyshare\temp\test.txt' is denied.

The application pool for this site is running under a domain account (MYCOMPANY\adminacct). MYCOMPANY\adminacct has access to the file \companyshare\temp\test.txt. I've confirmed that by logging into the server with the MYCOMPANY\adminacct credentials and navigating to the file using the same path that is in the code.

I'm running IIS 7.5

Questions

How come the above code is still getting access denied even though the application pool account has access to the folder and file?

Is there a way to confirm that my code is actually trying to access that file with the credentials I believe it is using?

Upvotes: 2

Views: 1895

Answers (1)

KP.
KP.

Reputation: 13720

To debug, the first thing I'd try is using impersonation in the web.config, to force the ASP.NET process to run as yourself, or an account you can guarantee has access to the UNC file path.

<identity impersonate="true" userName="yourDomain\yourAccountName" password="xyz" />

Personally, I'd choose to use impersonation in the application over credentials in the IIS app pool, but that's me.

Here's a good article on server process identity and debugging I've used several times.

Upvotes: 1

Related Questions