HTTPClient getting two 401s before success (sending wrong token)

后端 未结 3 2013
说谎
说谎 2021-02-07 11:14

I\'m trying to communicate with a self-hosted WebAPI client using HttpClient. The client is created with the following code:

HttpClientHandler clien         


        
3条回答
  •  情深已故
    2021-02-07 12:00

    Faced a similar issue, and after going through a lot of answers, where none of those worked. The following worked, and it doesn't come up with two 401's:

    var credential = new NetworkCredential("username", "password", "domainname");
    var myCache = new CredentialCache();
    
    // Add the target Uri to the CredentialCache with credential object
    myCache.Add(new Uri("http://targeturi/"), "NTLM", credential);
    
    // Create an HttpClientHandler to add some settings
    var handler = new HttpClientHandler();
    handler.AllowAutoRedirect = true;
    handler.Credentials = myCache;
    
    // Create an HttpClient with the handler object
    httpClient = new HttpClient(handler);
    
    // Wait to get the reponse, and you can use the reponse in your code
    HttpResponseMessage response = await httpClient.GetAsync(resourceUri);
    

    Source

提交回复
热议问题