C# HttpClient Digest认证不工作

问题描述 投票:0回答:1

我有.NET标准2.1库与HTTP API进行通信,并使用digest认证。下面是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();

问题是,第一个请求的回答是:

HTTP1.1 401 UnauthorizedServer: Web Server2.1.0 PeerSec-MatrixSSL3.9.5OPENDate: Wed Jun 3 14:04:34 2020WWW-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-cacheCache-Control:no-cacheContent-Type:texthtml。

文档错误。UnauthorizedAccess Error: UnauthorizedAccess这个文件需要一个用户ID。

httpclient必须在适当的授权下进行第二次请求,但第二次请求没有发送。我找了两天都没有成功。有没有人知道这个问题的解决方案。T试试这个问题的解决方案。.Net Core HttpClient Digest Authentication. 但对我来说并不适用。HTTP API与POSTMAN和网络浏览器(Firefox,Edge等)正常工作。

c# authentication hash httpclient
1个回答
0
投票

我不知道哪里出了问题,但问题的解决方案。.Net Core HttpClient Digest Authentication 作品。

© www.soinside.com 2019 - 2024. All rights reserved.