【问题标题】:C# HttpClient Authentication not working (AXIS Cam cgi API)C# HttpClient 身份验证不起作用(AXIS Cam cgi API)
【发布时间】:2016-10-03 07:44:40
【问题描述】:

我尝试了很多不同的方法来进行身份验证,但都没有奏效,要么程序崩溃,要么我没有得到响应:

async void GetRequest(string url)
    {
        //Test 1
        /*var credentials = new NetworkCredential("root", "root");
        var handler = new HttpClientHandler { Credentials = credentials };*/

        //Test 2
        /*var credCache = new CredentialCache();
        credCache.Add(new Uri(url), "Digest", new NetworkCredential("root", "root"));
        var handler = new HttpClientHandler { Credentials = credCache };*/

        using (HttpClient client = new HttpClient(/*handler*/))
        {
            //Test 3
            /*var encoding = new ASCIIEncoding();
            var authHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(encoding.GetBytes(string.Format("{0}:{1}", "root", "root"))));
            client.DefaultRequestHeaders.Authorization = authHeader;*/

            //Test 4 & 5
            /*var headerVal = Convert.ToBase64String(Encoding.ASCII.GetBytes("root:root"));
            var headerVal = Convert.ToBase64String(Encoding.UTF8.GetBytes("root:root"));
            var header = new AuthenticationHeaderValue("Basic", headerVal);
            client.DefaultRequestHeaders.Authorization = header;*/

            using (HttpResponseMessage response = await client.GetAsync(url))
            {
                using (HttpContent content = response.Content)
                {
                    string mycontent = await content.ReadAsStringAsync();
                    Debug.WriteLine(mycontent);
                }
            }
        }
    }

以下是我在 Web 浏览器中尝试链接 (http://192.168.68.127/axis-cgi/param.cgi) 时的一些请求标头和响应,有无密码用户名:

(Windows 安全

服务器 192.168.68.127 正在询问您的用户名和密码。服务器报告它来自 AXIS_00408C9A1F56。)

没有:

HTTP/1.1 401 Unauthorized
Date: Mon, 03 Oct 2016 07:31:44 GMT
Accept-Ranges: bytes
Connection: close
WWW-Authenticate: Digest realm = "AXIS_00408C9A1F56",
nonce = "0010e555Y3035383167c1586f9d3cbd5827c202d485cb9", stale = FALSE,
qop = "auth"
Basic realm = "AXIS_00408C9A1F56"
Content-Length: 184
Content-Type: text/html; charset=ISO-8859-1


GET /axis-cgi/param.cgi HTTP/1.1
Host: 192.168.68.127
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 
Firefox/49.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1


<HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>
<BODY><H1>401 Unauthorized</H1>
Your client does not have permission to get URL /axis-cgi/param.cgi from 
this server.
</BODY></HTML>

与:

HTTP/1.1 200 OK
Date: Mon, 03 Oct 2016 07:29:46 GMT
Accept-Ranges: bytes
Connection: close
Authentication-Info: qop=auth, rspauth="aa088ab10537d01e434856d0bab92930", 
cnonce="1a005dc72160cede", nc=00000002
Content-Type: text/plain


GET /axis-cgi/param.cgi HTTP/1.1
Host: 192.168.68.127
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 
Firefox/49.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Authorization: Digest username="root", realm="AXIS_00408C9A1F56", 
nonce="0010e4b2Y82490734fa5d5f4f6ccc48546da4c59d6fc85", uri="/axis-
cgi/param.cgi", response="dfb04200dafbce37994999dd219c5dd8", qop=auth, 
nc=00000002, cnonce="1a005dc72160cede"
Connection: keep-alive
Upgrade-Insecure-Requests: 1


root.MediaClip.MaxGroups=10
root.MediaClip.M0.Name=Burglar_Alarm_Short
root.MediaClip.M0.Location=/etc/audioclips/Burglar_Alarm_Short_8bit_32kHz_mono_PCM-16bit-little.wav
root.MediaClip.M0.Type=audio
root.MediaClip.M6.Name=Camera clicks
root.MediaClip.M6.Location=/etc/audioclips/camera_clicks16k.au
root.MediaClip.M6.Type=audio
root.MediaClip.M7.Name=Pssst, Psst!
root.MediaClip.M7.Location=/etc/audioclips/pssst_psst16k.au
root.MediaClip.M7.Type=audio
root.MediaClip.M8.Name=Intruder
root.MediaClip.M8.Location=/etc/audioclips/intruder16k.au
root.MediaClip.M8.Type=audio
root.MediaClip.M9.Name=Dog barking
root.MediaClip.M9.Location=/etc/audioclips/dog_barking16k.au
root.MediaClip.M9.Type=audio

【问题讨论】:

  • 仅供参考:192.168.X.Y 位于私人地址区域。因此其他用户无法访问您的链接。
  • 是的,我知道,这是专用网络上的 AXIS 摄像机,无法从外部访问。

标签: c# api authentication httpclient axis


【解决方案1】:

您可以执行以下操作:

var uri = new UriBuilder("http://192.168.68.127")
{
     Path = "axis-cgi/param.cgi",
     Query = "action=list"
};
var request = (HttpWebRequest)WebRequest.Create(uri.Uri);
request.Credentials = new NetworkCredential("root", "root");
using (var response = await request.GetResponseAsync())
{
}

我必须提到,对于我拥有的一些摄像头和一些请求,摄像头端存在 CRLF 问题,因此 app.config 或 web.config 中的参数 useUnsafeHeaderParsing 可能有用:

<system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
</system.net>

【讨论】:

  • 哦...,实际上我所有的“测试”都在使用“useUnsafeHeaderParsing”设置...非常感谢! :)
猜你喜欢
  • 2020-09-23
  • 1970-01-01
  • 2020-03-13
  • 2018-05-24
  • 2017-09-11
  • 2012-10-04
  • 1970-01-01
相关资源
最近更新 更多