0

I'm trying to convert a Python script to C# but have hit a problem.

The python script uses HTTPDigestAuth to authrenticate the for the API

r = requests.get(apiUrl,auth=HTTPDigestAuth(apiuser, apipass))

I've tried the following in C# but it clearly not authenticating correctly as when I look at the headers after the call its showing as DENY, yet the exact same credentials in Python work perfectly.

Here is the code I'm trying below.

string url = "www.test.com";

string UserName = "xx";
string Password = "L12312321312";

NetworkCredential wrCred = new NetworkCredential(UserName, Password);

WebRequest myWebRequest = WebRequest.Create(url);
myWebRequest.Method = "GET";
myWebRequest.Credentials = wrCred;
myWebRequest.PreAuthenticate = true;

//Get Reponse
WebResponse myWebResponse = myWebRequest.GetResponse();

//Data stream
Stream dataStream = myWebResponse.GetResponseStream();

// Open the stream using a StreamReader for easy access.  
StreamReader reader = new StreamReader(dataStream);

// Read the content.  
string responseFromServer = reader.ReadToEnd();

//Check
MessageBox.Show(myWebResponse.Headers.ToString());

reader.Close();
myWebResponse.Close();

is there something I'm missing as the header results are the same as if I alter the python script to use basic auth instead (like below) so its like its not using Digest Auth and just defaulting to Basic.

r = requests.get(apiUrl,auth=HTTPBasicAuth(apiuser, apipass))

If I return the status code for the webresponse it returning OK but no body or content is returned again which makes me think its not using Digest Auth.

Jaliya Udagedara
  • 1,087
  • 10
  • 16
MrG
  • 55
  • 1
  • 1
  • 8

0 Answers0