0

I need to authorize myself to get some info from an API. The thing is when I try to authorize myself it's giving me 401 error. I used as base the next post, which I adapted to fit my system. https://stackoverflow.com/a/30873757/10279381

            uri = new Uri(urlBase + dir);
            var request2 = (HttpWebRequest)WebRequest.Create(uri);
            request2.Headers.Add("Authorization", GetDigestHeader(dir));
            request2.Accept = "text/html,application/xhtml+xml,application/xml";
            response = (HttpWebResponse)request2.GetResponse();

The thing is GetDigestHeader method returns the right authorization string (I tried to use it using postman and it runs ok), but when I run my code it returns code 401. Is there something I'm missing? Postman has no other headers than authorization, and I have tried my code with and without Accepts header.

EDIT:

GET /18 HTTP/1.1
Host: 10.0.20.109
Authorization: Digest username="admin", realm="iRMC S4@iRMC00D83D", nonce="1b4e8277-00021548", uri="/18", algorithm=MD5, response="93c149aed13d2a8d0c8ec5ac7d7f052d"
Cache-Control: no-cache

1 Answers1

0

Did you try using HttpClient

HttpClientObject = new HttpClient(new HttpClientHandler 
{ AutomaticDecompression = DecompressionMethods.GZip, 
ClientCertificateOptions = ClientCertificateOption.Automatic, 
Credentials = new NetworkCredential(username, password, domain) });
HttpClientObject.BaseAddress = new Uri(url);
HttpClientObject.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html,application/xhtml+xml,application/xml"));
HttpResponseMessage response = HttpClientObject.GetAsync();