简体   繁体   English

C# HttpClient 摘要身份验证不起作用

[英]C# HttpClient Digest Authentication not work

I have .NET Standard 2.1 library to communicate with HTTP API with digest authentication.我有 .NET 标准 2.1 库与 HTTP API 进行通信与摘要身份验证。 Here is C# code:这是 C# 代码:

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 HTTP/1.1 401 未经授权的服务器:Web 服务器/2.1.0 PeerSec-MatrixSSL/3.9.5-OPEN 日期:2020 年 6 月 3 日星期三 14:04:34 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:缓存缓存控制:无缓存内容类型:文本/html

Document Error: Unauthorized Access Error: Unauthorized Access to this document requires a User ID文档错误:未经授权的访问错误:未经授权访问此文档需要用户 ID

The httpclient have to make second request with proper authorization but second request is not send. httpclient 必须通过适当的授权发出第二个请求,但不会发送第二个请求。 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. T尝试这个问题的解决方案: .Net Core HttpClient Digest Authentication但对我不起作用。 The HTTP API works correctly with POSTMAN and web browser(Firefox, Edge etc.). HTTP API 与 POSTMAN 和 Z2567A5EC9705EB7AC2C984033E06189 等浏览器正常工作。

I don't know where was the problem but solution in question: .Net Core HttpClient Digest Authentication works.我不知道问题出在哪里,但有问题的解决方案: .Net Core HttpClient Digest Authentication有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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