简体   繁体   中英

C# HttpClient Authentication not working (AXIS Cam cgi API)

I tried a lot of different methods to authenticate but non of them worked, either the program crashed or I got no response:

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);
                }
            }
        }
    }

Here are some request headers and response when I try the link ( http://192.168.68.127/axis-cgi/param.cgi ) in my web browser with and without a password-username:

(Windows Security

The server 192.168.68.127 is asking for your username and password. The server reports that it is from AXIS_00408C9A1F56.)

Without:

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>

With:

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

You could do the following:

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())
{
}

I must mention that with some of the cameras I have, and some requests, there are CRLF problems on the camera-side so the parameter useUnsafeHeaderParsing in app.config or web.config might be useful:

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

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