The Scenario
I have two applications running on separate servers, within the same internal network.
Server 1 contains a WebAPI MVC Project.
Server 2 contains a MVC Web Application which calls the API on server 1.
Both Applications work using Windows Authentication.
The code works fine when I'm running them locally, both through IIS and running through Visual Studio.
When running on the server I get the following error:
The remote server returned an error: (401) Unauthorized.
Below is the request from Fiddler.
GET http://server1/..../..../GetTest HTTP/1.1
Host: server 1
Connection: keep-alive
Cache-Control: max-age=0
Authorization: Negotiate YIIHIgYGKwYBBQUCoIIHFjCCBxK.....
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Cookie: ASP.NET_SessionId=gffyzdgub41op0diygy5lyv2
Below is the API request code
public string GetTest()
{
string sURL = "http://server2/.../api/test?value=JERONIMO";
WebRequest wrGETURL = WebRequest.Create(sURL);
wrGETURL.Credentials = CredentialCache.DefaultCredentials;
var result = "";
using (Stream objStream = wrGETURL.GetResponse().GetResponseStream()) {
using(StreamReader objReader = new StreamReader(objStream)) {
string sLine = "";
int i = 0;
while (sLine != null) {
i++;
sLine = objReader.ReadLine();
if (sLine != null) result += sLine;
}
}
}
return result;
}
Thanks for the help.