Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.IO;
  11. using System.Security.Cryptography;
  12. using System.Text.RegularExpressions;
  13. using System.Security.Cryptography.X509Certificates;
  14. using System.Net.Security;
  15.  
  16. namespace test2
  17. {
  18. public partial class Form1 : Form
  19. {
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25.  
  26. public class DigestAuthFixer
  27. {
  28. private static string _host;
  29. private static string _user;
  30. private static string _password;
  31. private static string _realm;
  32. private static string _nonce;
  33. private static string _qop;
  34. private static string _cnonce;
  35. private static DateTime _cnonceDate;
  36. private static int _nc;
  37.  
  38. public DigestAuthFixer(string host, string user, string password)
  39. {
  40. // TODO: Complete member initialization
  41. _host = host;
  42. _user = user;
  43. _password = password;
  44. }
  45.  
  46. private string CalculateMd5Hash(
  47. string input)
  48. {
  49. var inputBytes = Encoding.ASCII.GetBytes(input);
  50. var hash = MD5.Create().ComputeHash(inputBytes);
  51. var sb = new StringBuilder();
  52. foreach (var b in hash)
  53. sb.Append(b.ToString("x2"));
  54. return sb.ToString();
  55. }
  56.  
  57. private string GrabHeaderVar(
  58. string varName,
  59. string header)
  60. {
  61. var regHeader = new Regex(string.Format(@"{0}=""([^""]*)""", varName));
  62. var matchHeader = regHeader.Match(header);
  63. if (matchHeader.Success)
  64. return matchHeader.Groups[1].Value;
  65. throw new ApplicationException(string.Format("Header {0} not found", varName));
  66. }
  67.  
  68. private string GetDigestHeader(
  69. string dir)
  70. {
  71. _nc = _nc + 1;
  72.  
  73. var ha1 = CalculateMd5Hash(string.Format("{0}:{1}:{2}", _user, _realm, _password));
  74. var ha2 = CalculateMd5Hash(string.Format("{0}:{1}", "GET", dir));
  75. var digestResponse =
  76. CalculateMd5Hash(string.Format("{0}:{1}:{2:00000000}:{3}:{4}:{5}", ha1, _nonce, _nc, _cnonce, _qop, ha2));
  77.  
  78. return string.Format("Digest username=\"{0}\", realm=\"{1}\", nonce=\"{2}\", uri=\"{3}\", " +
  79. "algorithm=MD5, response=\"{4}\", qop={5}, nc={6:00000000}, cnonce=\"{7}\"",
  80. _user, _realm, _nonce, dir, digestResponse, _qop, _nc, _cnonce);
  81. }
  82.  
  83. public string GrabResponse(
  84. string dir)
  85. {
  86. var url = _host + dir;
  87. var uri = new Uri(url);
  88.  
  89. var request = (HttpWebRequest)WebRequest.Create(uri);
  90.  
  91. // If we've got a recent Auth header, re-use it!
  92. if (!string.IsNullOrEmpty(_cnonce) &&
  93. DateTime.Now.Subtract(_cnonceDate).TotalHours < 1.0)
  94. {
  95. request.Headers.Add("Authorization", GetDigestHeader(dir));
  96. }
  97.  
  98. HttpWebResponse response;
  99. try
  100. {
  101. response = (HttpWebResponse)request.GetResponse();
  102. }
  103. catch (WebException ex)
  104. {
  105. // Try to fix a 401 exception by adding a Authorization header
  106. if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.Unauthorized)
  107. throw;
  108.  
  109. var wwwAuthenticateHeader = ex.Response.Headers["WWW-Authenticate"];
  110. _realm = GrabHeaderVar("realm", wwwAuthenticateHeader);
  111. _nonce = GrabHeaderVar("nonce", wwwAuthenticateHeader);
  112. _qop = GrabHeaderVar("qop", wwwAuthenticateHeader);
  113.  
  114. _nc = 0;
  115. _cnonce = new Random().Next(123400, 9999999).ToString();
  116. _cnonceDate = DateTime.Now;
  117.  
  118. var request2 = (HttpWebRequest)WebRequest.Create(uri);
  119. request2.Headers.Add("Authorization", GetDigestHeader(dir));
  120. response = (HttpWebResponse)request2.GetResponse();
  121. }
  122. var reader = new StreamReader(response.GetResponseStream());
  123. return reader.ReadToEnd();
  124. }
  125.  
  126. }
  127. public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  128. {
  129. return true;
  130. }
  131. private void button1_Click(object sender, EventArgs e)
  132. {
  133. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
  134. DigestAuthFixer digest = new DigestAuthFixer("https://118.70.72.171:8443", "cdrapi", "cdrapi123");
  135. string strReturn = digest.GrabResponse("/cdrapi?format=xml&startTime=2017-03-03T22:01:06&endTime=now&numRecords=68");
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement