title: C# HttpClient Authentication not working (AXIS Cam cgi API)
body: <p>I tried a lot of different methods to authenticate but non of them worked, either the program crashed or I got no response:</p>
  
  <pre><code>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 &amp; 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);
                  }
              }
          }
      }
  </code></pre>
  
  <p>Here are some request headers and response when I try the link (<a href="http://192.168.68.127/axis-cgi/param.cgi" rel="nofollow">http://192.168.68.127/axis-cgi/param.cgi</a>) in my web browser with and without a password-username:</p>
  
  <p>(Windows Security</p>
  
  <p>The server 192.168.68.127 is asking for your username and password. The server reports that it is from AXIS_00408C9A1F56.)</p>
  
  <p>Without:</p>
  
  <pre><code>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
  
  
  &lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;401 Unauthorized&lt;/TITLE&gt;&lt;/HEAD&gt;
  &lt;BODY&gt;&lt;H1&gt;401 Unauthorized&lt;/H1&gt;
  Your client does not have permission to get URL /axis-cgi/param.cgi from 
  this server.
  &lt;/BODY&gt;&lt;/HTML&gt;
  </code></pre>
  
  <p>With:</p>
  
  <pre><code>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
  </code></pre>