How can I do digest authentication with HttpWebRequest?

前端 未结 4 1852
一整个雨季
一整个雨季 2020-12-08 21:08

Various articles (1, 2) I discovered make this look easy enough:

WebRequest request = HttpWebRequest.Create(url);

var credentialCache = new CredentialCache(         


        
4条回答
  •  失恋的感觉
    2020-12-08 21:51

    You said you removed the querystring paramters, but did you try going all the way back to just the host? Every single example of CredentialsCache.Add() I've seen seems to use only the host, and the docs for CredentialsCache.Add() list the Uri parameter as "uriPrefix", which seems telling.

    In other words, try this out:

    Uri uri = new Uri(url);
    WebRequest request = WebRequest.Create(uri);
    
    var credentialCache = new CredentialCache(); 
    credentialCache.Add( 
      new Uri(uri.GetLeftPart(UriPartial.Authority)), // request url's host
      "Digest",  // authentication type 
      new NetworkCredential("user", "password") // credentials 
    ); 
    
    request.Credentials = credentialCache;
    

    If this works, you will also have to make sure that you don't add the same "authority" to the cache more than once... all requests to the same host should be able to make use of the same credential cache entry.

提交回复
热议问题