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

后端 未结 3 2015
说谎
说谎 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 11:52

    What you are experiencing is normal, this is how the NTLM authentication scheme works.

    1: C  --> S   GET ...
    
    2: C <--  S   401 Unauthorized
                  WWW-Authenticate: NTLM
    
    3: C  --> S   GET ...
                  Authorization: NTLM <base64-encoded type-1-message>
    
    4: C <--  S   401 Unauthorized
                  WWW-Authenticate: NTLM <base64-encoded type-2-message>
    
    5: C  --> S   GET ...
                  Authorization: NTLM <base64-encoded type-3-message>
    
    6: C <--  S   200 Ok
    
    1. The client sends a GET request to the server.
    2. Since you need to be authenticated to access the requested resource, the server sends back a 401 Unathorized response and notifies the client in the WWW-Authenticate header that it supports NTLM authentication. So this is where you get your first 401 response code.
    3. The client sends the domain name and the username to the server in the Authorization header. Note that based solely on these information the client cannot be authenticated yet.
    4. The server sends a challenge to the client. It's a randomly generated number called a nonce. This is where you get your second 401 response code.
    5. The client sends back a response to the server's challenge, using its password's hash to encrypt the random number.
    6. The server sends the client's username, the challenge sent to the client and the response received from the client to the domain controller. Using the username the domain controller retrieves the hash of the user's password and encrypts the challenge with it. If the result matches the response sent by the client, the client is authenticated and the server sends back a 200 response code and the requested resource to the client.
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-07 12:00

    the default behavior where it only sends credentials after receiving an HTTP 401 Not Authorized response.

    Manually adding the credentials header seems to be the best solution available.

    More details in this post

    0 讨论(0)
提交回复
热议问题