Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. public void DoIT()
  2. {
  3. try
  4. {
  5.  
  6. HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
  7. StreamReader responseReader = new StreamReader(
  8. webRequest.GetResponse().GetResponseStream()
  9. );
  10. string responseData = responseReader.ReadToEnd();
  11. responseReader.Close();
  12.  
  13. // extract the viewstate value and build out POST data
  14. string viewState = ExtractViewState(responseData);
  15. string EventValidation = ExtractEventValidation(responseData);
  16. string viewstategen = ExtractViewStateGen(responseData);
  17.  
  18. NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
  19. outgoingQueryString.Add("__VIEWSTATE", viewState);
  20. outgoingQueryString.Add("__EVENTVALIDATION", EventValidation);
  21. outgoingQueryString.Add("__VIEWSTATEGENERATOR", viewstategen);
  22. outgoingQueryString.Add("__EVENTTARGET", string.Empty);
  23. outgoingQueryString.Add("__EVENTARGUMENT", string.Empty);
  24.  
  25. outgoingQueryString.Add("Login1$UserName", "TestUser");
  26. outgoingQueryString.Add("Login1$Password", "TestPass");
  27. outgoingQueryString.Add("Login1$SiteDDL", "TestSite");
  28. outgoingQueryString.Add("ShowHospreferral", "true");
  29.  
  30.  
  31. outgoingQueryString.Add("Login1$LoginButton", "Login");
  32.  
  33. var data = Encoding.ASCII.GetBytes(outgoingQueryString.ToString());
  34. string postData =
  35. String.Format(
  36. "__VIEWSTATE={0}& __EVENTVALIDATION={1}&__EVENTTARGET={2}&__EVENTARGUMENT={3}&__VIEWSTATEGENERATOR={4}&Login1$UserName={5}&Login1$Password={6}&Login1$SiteDDL={6}&ShowHospreferral={7}&Login1$LoginButton={8}",
  37. viewState ,EventValidation, HttpUtility.UrlEncodeUnicode(string.Empty), HttpUtility.UrlEncodeUnicode(string.Empty), viewstategen , "Dougie", "Cardiology1", "T101H", HttpUtility.UrlEncodeUnicode("true"), "Login"
  38. );
  39.  
  40. // have a cookie container ready to receive the forms auth cookie
  41. CookieContainer cookies = new CookieContainer();
  42.  
  43. // now post to the login form
  44. webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
  45. webRequest.Method = "POST";
  46. webRequest.ContentType = "application/x-www-form-urlencoded";
  47. webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
  48. webRequest.Headers.Set("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8");
  49. webRequest.Headers.Set("Accept-Encoding", "gzip, deflate, br");
  50. webRequest.Referer = LOGIN_URL;
  51. webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
  52. webRequest.CookieContainer = cookies;
  53.  
  54. // write the form values into the request message
  55.  
  56.  
  57.  
  58.  
  59. StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
  60. requestWriter.Write(postData);
  61. requestWriter.Close();
  62.  
  63. // we don't need the contents of the response, just the cookie it issues
  64. webRequest.GetResponse().Close();
  65.  
  66. // now we can send out cookie along with a request for the protected page
  67. webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
  68. webRequest.CookieContainer = cookies;
  69. responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
  70.  
  71. // and read the response
  72. responseData = responseReader.ReadToEnd();
  73. responseReader.Close();
  74.  
  75. textBox1.Text = responseData;
  76. }
  77. catch (WebException webex)
  78. {
  79. WebResponse errResp = webex.Response;
  80. using (Stream respStream = errResp.GetResponseStream())
  81. {
  82. StreamReader reader = new StreamReader(respStream);
  83. textBox1.Text = reader.ReadToEnd();
  84. }
  85. }
  86. catch (Exception ex)
  87. { textBox1.Text = ex.Message; }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement