简体   繁体   中英

C# HttpClient Digest Authentication not work

I have .NET Standard 2.1 library to communicate with HTTP API with digest authentication. Here is C# code:

var credCache = new CredentialCache
{
    {
        new Uri($"http://192.168.13.100/"),
        "Digest",
        new NetworkCredential("admin", "admin")
    }
};

var httpClientHandler = new HttpClientHandler
{
    PreAuthenticate = true,
    Credentials = credCache
};
httpClient = new HttpClient(httpClientHandler);

SendSMS sendSms = new SendSMS()
{
    text = message,
    param = new SendSMSParam[1] {
        new SendSMSParam()
        {
            number = phone,
            user_id = messageId
        }
    },
    port = new int[1] { port },
    encoding = "unicode",
    request_status_report = true,
};

string json = JsonConvert.SerializeObject(sendSms);

HttpResponseMessage response = await httpClient.PostAsync("http://192.168.13.100/api/send_sms", new StringContent(json));
string answer = await response.Content.ReadAsStringAsync();

The problem is that answer from first request is:

HTTP/1.1 401 Unauthorized Server: Web Server/2.1.0 PeerSec-MatrixSSL/3.9.5-OPEN Date: Wed Jun 3 14:04:34 2020 WWW-Authenticate: Digest realm="Web Server", domain="",qop="auth", nonce="7d65e9cdaa4eea9a6b12d10bda58269a", opaque="5ccc069c403ebaf9f0171e9517f40e41",algorithm="MD5", stale="FALSE" Set-Cookie: devckie=db39-a230-9038-0160;path=/ Pragma: no-cache Cache-Control: no-cache Content-Type: text/html

Document Error: Unauthorized Access Error: Unauthorized Access to this document requires a User ID

The httpclient have to make second request with proper authorization but second request is not send. I search for solution about two days without success. Does someone know solution about this problem. T try solution from this question: .Net Core HttpClient Digest Authentication but not work for me. The HTTP API works correctly with POSTMAN and web browser(Firefox, Edge etc.).

I don't know where was the problem but solution in question: .Net Core HttpClient Digest Authentication works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM