【问题标题】:C# HttpClient Digest Authentication not workC# HttpClient 摘要身份验证不起作用
【发布时间】:2020-09-23 04:05:18
【问题描述】:

我有 .NET Standard 2.1 库,可以通过摘要身份验证与 HTTP API 进行通信。 这是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();

问题是第一个请求的答案是:

HTTP/1.1 401 未经授权 服务器:Web Server/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" 设置 Cookie:devckie=db39-a230-9038-0160;path=/ Pragma:无缓存 缓存控制:无缓存 内容类型:text/html

文档错误:未经授权 访问错误:未经授权 访问此文档需要用户 ID

httpclient 必须通过适当的授权发出第二个请求,但第二个请求没有发送。我搜索了大约两天没有成功的解决方案。有人知道这个问题的解决方案吗。 T 尝试解决这个问题:.Net Core HttpClient Digest Authentication 但对我不起作用。 HTTP API 可以在 POSTMAN 和网络浏览器(Firefox、Edge 等)上正常工作。

【问题讨论】:

    标签: c# authentication hash httpclient


    【解决方案1】:

    我不知道问题出在哪里,但有问题的解决方案:.Net Core HttpClient Digest Authentication 有效。

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多