I'm hoping someone can help me with this as I am at my wits' end.
I have a simple Web API web service that checks for the existence of a workspace on our document management system to determine security access.
When I run this service in the browser it works fine. However when I try to use the service in another Web API application it gives a 401.
I've tried pretty much everything I can think of and can find on here. It is using Windows Authentication and no matter what I try, NTLM, Negotiate or Negotiate:Kerberos, the credentials of the current user are not passed through. If I hard code my userid, password and domain however it works. The code is below
[HttpGet]
[Route("api/Session/CheckSecurity")]
public HttpResponseMessage CheckSecurity(string id)
{
WebClient client;
WindowsIdentity userId;
string result;
try
{
userId = (WindowsIdentity)User.Identity;
using (WindowsImpersonationContext context = userId.Impersonate())
{
client = new System.Net.WebClient() { UseDefaultCredentials = true };
client.Headers.Add("content-type", "application/json");
result = client.DownloadString(new Uri(String.Format(ConfigurationManager.AppSettings["Url"], id)));
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
catch (WebException webExcp)
{
HttpWebResponse resp = (HttpWebResponse)webExcp.Response;
resp.Headers.Remove("WWW-Authenticate");
// return 403 instead of 401 to prevent security pop up
return new HttpResponseMessage(resp.StatusCode == HttpStatusCode.Unauthorized ? HttpStatusCode.Forbidden : resp.StatusCode);
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
contextobject but don't use it in theWebClient, is that intentional?