title: ASP.NET HttpWebRequest with Kerberos Authentication
body: <p>I am trying to connect to a web service that uses Kerberos Authentication to authorize the user, but all I get is a 401 unauthorized everytime I try to make the request. Below is the code that I am using. Thanks in advance for any help you can provide!</p>
  
  <pre><code>    public XPathNavigator GSASearch(string url, string searchString)
      {
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + searchString);
          request.CookieContainer = new CookieContainer();
          request.Credentials = CredentialCache.DefaultCredentials;
          request.ContentType = "text/xml";
          request.Method = "POST";
  
          HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  
          Stream receiveStream = response.GetResponseStream();
  
          XPathDocument doc = new XPathDocument(receiveStream);
          return doc.CreateNavigator();
      }
  </code></pre>
  
  <p>EDIT: I feel I should explain a bit more what I am attempting to do. I have been tasked with providing a new interface for my company's Google Search Appliance. I am using an ASP.NET page, which does some things like choose a Collection depending on where a user is located, etc. and then sends the appropriate search string the the GSA. This was all working well until they decided to turn authentication on, and now I can't get any results (I either get a 401 unauthorized, or a message stating that 'Data at the root level is invalid'). If I take the search string and provide it directly to the GSA, it authenticates fine, and displays the results, I just can't seem to get it through the HttpWebRequest.</p>
  
  <p>EDIT 2: I did a little more looking (ran the request through Fiddler) and it looks like the request is only attempting Negotiate and not Kerberos. I set the credentials to use Kerberos explicitly as below, but it didn't help...</p>
  
  <pre><code>    public XPathNavigator GSASearch(string url, string searchString)
      {
          CredentialCache credCache = new CredentialCache();
          credCache.Add(new Uri(url), "Kerberos", CredentialCache.DefaultNetworkCredentials);
  
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + searchString);
          request.CookieContainer = new CookieContainer();
          request.PreAuthenticate = true;
          request.Credentials = credCache;
          request.ContentType = "text/xml";
          request.Method = "POST";
  
          HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  
          Stream receiveStream = response.GetResponseStream();
  
          //StreamReader readStream = new StreamReader(receiveStream);
  
          XPathDocument doc = new XPathDocument(receiveStream);
          return doc.CreateNavigator();
      }
  </code></pre>
  
  <p>EDIT 3: Ok, looking closer again, the CredentialCache.DefaultCredentials doesn't appear to have my network credentials in it...</p>