IIS cannot write file if called from remote, only from the server

871 views Asked by At

I am running a web api on a IIS on Windows Server 2012 R2.

The application has a controller, that on a get request will write to a file on a network share.

If I access the site from a browser on the server it works fine, however if I access the site from my own computer, I get an exception saying that I do not have permission to write to the file.

Users are authenticated against the networks AD, and I am using the same user on both my computer and on the server.

It seems that IIS is handling the request as an other user if I call it from the server, but I have not been able to figure out how I can change this so I can write the file from another host as well.

The code that is giving the exception is using a StreamWriter

using (var writer = new StreamWriter(_filePathName))
{
    writer.WriteLine(GetSetting("PROTECTED_KMDHeader"));
    writer.Close();
}
1

There are 1 answers

2
Tien Dinh On BEST ANSWER

You need to enable impersonation. See this link https://technet.microsoft.com/en-au/library/cc730708(v=ws.10).aspx