By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Form Login Help

100+
P: 233
I am attempting to use WebRequest to access a page that requires a login/password to access. My last WebRequest continues to timeout. Any help or thoughts would be appreciated.

Expand|Select|Wrap|Line Numbers
  1.  
  2. namespace FormsAuthTest
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. HttpWebRequest request = null;
  9. HttpWebResponse response = null;
  10. StreamReader sr = null;
  11. string originalUri = https://springboard.4imprint.com/PO/...112-46E9573F62;
  12. System.Diagnostics.ConsoleTraceListener trace =
  13. new System.Diagnostics.ConsoleTraceListener();
  14. //
  15. // Request page protected by forms authentication.
  16. // This request will get a 302 to login page
  17. //
  18. trace.Write("Requesting : " + originalUri);
  19. request = (HttpWebRequest)WebRequest.Create(originalUri);
  20. request.CookieContainer = new CookieContainer();
  21. request.AllowAutoRedirect = false;
  22. response = (HttpWebResponse)request.GetResponse();
  23. if (response.StatusCode == HttpStatusCode.Found)
  24. {
  25. trace.Write("Response: 302 ");
  26. trace.WriteLine(response.StatusCode);
  27. }
  28. else
  29. {
  30. trace.Fail("Response status is " + response.StatusCode + ". Expected was Found");
  31. }
  32. //
  33. // Get the url of login page from location header
  34. //
  35. String locationHeader = response.GetResponseHeader("Location");
  36. trace.WriteLine("Location header is " + locationHeader);
  37. trace.WriteLine("");
  38. //
  39. // Request login page
  40. //
  41. String loginPageUrl = "https://springboard.4imprint.com" + locationHeader;
  42. Console.WriteLine("Requesting " + loginPageUrl);
  43. request = (HttpWebRequest)WebRequest.Create(loginPageUrl);
  44. request.CookieContainer = new CookieContainer();
  45. request.AllowAutoRedirect = false;
  46. response = (HttpWebResponse)request.GetResponse();
  47. if (response.StatusCode == HttpStatusCode.OK)
  48. {
  49. trace.Write("Response: 200 ");
  50. trace.WriteLine(response.StatusCode);
  51. }
  52. else
  53. {
  54. trace.Fail("Response status is " + response.StatusCode + ". Expected was OK");
  55. }
  56. trace.WriteLine("Parsing login page to create post message");
  57. trace.WriteLine("");
  58. sr = new StreamReader(response.GetResponseStream());
  59. String loginResponse = sr.ReadToEnd();
  60. sr.Close();
  61. String eventTargetVar = "__EVENTTARGET=";
  62. String eventTargetValue = "";
  63. String eventArgumentVar = "__EVENTARGUMENT=";
  64. String eventArgumentValue = "";
  65. String viewStateVar = "__VIEWSTATE=";
  66. String viewStateSearchString = "name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"";
  67. int viewStateStartIndex = loginResponse.IndexOf(viewStateSearchString);
  68. loginResponse = loginResponse.Substring(viewStateStartIndex + viewStateSearchString.Length);
  69. String viewStateValue = Uri.EscapeDataString(
  70. loginResponse.Substring(0, loginResponse.IndexOf("\" />"))
  71. );
  72. loginResponse = loginResponse.Substring(loginResponse.IndexOf("\" />"));
  73. String lcSearchStr = "input name=";
  74. int lcSearchIndex = 0;
  75. //
  76. // Look for logon control id
  77. // Use any valid username and password
  78. //
  79. lcSearchIndex = loginResponse.IndexOf(lcSearchStr);
  80. loginResponse = loginResponse.Substring(lcSearchIndex + lcSearchStr.Length + 1);
  81. String userNameVar = Uri.EscapeDataString(
  82. loginResponse.Substring(0, loginResponse.IndexOf("\""))
  83. ) + "=";
  84. String userNameValue = "username";
  85. lcSearchIndex = loginResponse.IndexOf(lcSearchStr);
  86. loginResponse = loginResponse.Substring(lcSearchIndex + lcSearchStr.Length + 1);
  87. String passwordVar = Uri.EscapeDataString(
  88. loginResponse.Substring(0, loginResponse.IndexOf("\""))
  89. ) + "=";
  90. String passwordValue = "password";
  91. lcSearchStr = "type=\"submit\" name=";
  92. lcSearchIndex = loginResponse.IndexOf(lcSearchStr);
  93. loginResponse = loginResponse.Substring(lcSearchIndex + lcSearchStr.Length + 1);
  94. String loginButtonVar = Uri.EscapeDataString(
  95. loginResponse.Substring(0, loginResponse.IndexOf("\""))
  96. ) + "=";
  97. String loginButtonValue = "Log+In";
  98. String eventValidationVar = "__EVENTVALIDATION=";
  99. String eventValSearchString =
  100. "name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"";
  101. int eventValStartIndex = loginResponse.IndexOf(eventValSearchString);
  102. loginResponse = loginResponse.Substring(eventValStartIndex + eventValSearchString.Length);
  103. String eventValidationValue =
  104. Uri.EscapeDataString(
  105. loginResponse.Substring(0, loginResponse.IndexOf("\" />"))
  106. );
  107. String postString = eventTargetVar + eventTargetValue;
  108. postString += "&" + eventArgumentVar + eventArgumentValue;
  109. postString += "&" + viewStateVar + viewStateValue;
  110. postString += "&" + userNameVar + userNameValue;
  111. postString += "&" + passwordVar + passwordValue;
  112. postString += "&" + loginButtonVar + loginButtonValue;
  113. postString += "&" + eventValidationVar + eventValidationValue;
  114. //
  115. // Do a POST to login.aspx now
  116. //
  117. Console.WriteLine("POST request to https://springboard.4imprint.com" + locationHeader);
  118. request = (HttpWebRequest)WebRequest.Create("https://springboard.4imprint.com" + locationHeader);
  119. request.CookieContainer = new CookieContainer();
  120. request.AllowAutoRedirect = false;
  121. request.Method = "POST";
  122. request.ContentType = "application/x-www-form-urlencoded";
  123. System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  124. byte[] requestData = encoding.GetBytes(postString);
  125. request.ContentLength = requestData.Length;
  126. Stream requestStream = request.GetRequestStream();
  127. requestStream.Write(requestData, 0, requestData.Length);
  128. requestStream.Close();
  129. response = (HttpWebResponse)request.GetResponse();
  130. if (response.StatusCode == HttpStatusCode.Found)
  131. {
  132. trace.Write("Response: 302 ");
  133. trace.WriteLine(response.StatusCode);
  134. }
  135. else
  136. {
  137. trace.Fail("Response status is " + response.StatusCode + ". Expected was Found");
  138. }
  139. locationHeader = response.GetResponseHeader("Location");
  140. trace.WriteLine("Location header is " + locationHeader);
  141. CookieCollection cookies = response.Cookies;
  142.  
  143.  
  144. //
  145.  
  146. // Send request to originalUri with the cookie
  147. // We should be able to see originalUri contents
  148. //
  149. trace.WriteLine("Requesting https://springboard.4imprint.com" + locationHeader + " with cookie");
  150. request = (HttpWebRequest)WebRequest.Create("https://springboard.4imprint.com" + locationHeader);
  151. request.CookieContainer = new CookieContainer();
  152. foreach(Cookie oneCookie in cookies)
  153. {
  154. request.CookieContainer.Add(oneCookie);
  155. }
  156. request.AllowAutoRedirect = false;
  157. //request.Headers.Add(HttpRequestHeader.Cookie, cookie);
  158. response = (HttpWebResponse)request.GetResponse();
  159. if (response.StatusCode == HttpStatusCode.OK)
  160. {
  161. trace.Write("Response: 200 ");
  162. trace.WriteLine(response.StatusCode);
  163. }
  164. else
  165. {
  166. trace.Fail("Response status is " + response.StatusCode + ". Expected was OK");
  167. }
  168. trace.WriteLine("");
  169. trace.WriteLine("Contents of " + originalUri);
  170. trace.WriteLine("");
  171. sr = new StreamReader(response.GetResponseStream());
  172. trace.WriteLine(sr.ReadToEnd());
  173. sr.Close();
  174. Console.Read();
  175. }
  176. }
  177. }
  178.  
Jan 21 '09 #1
Share this Question
Share on Google+
5 Replies


Plater
Expert 5K+
P: 7,872
Hmm. Are you remembering to add the Session cookie as well? Its not a regular cookie but a special header in the request.

Other then that I could only guess that you should get a packet watcher and see what data is being sent back and forth?
Jan 21 '09 #2

100+
P: 233
You are indeed correct, the session cookie was not being stored. I will have to look into how I can save the cookies from each response object and then add them to my final request.
Jan 21 '09 #3

100+
P: 233
So I have added the following to each HttpWebRequest:

Expand|Select|Wrap|Line Numbers
  1. request.CookieContainer = new CookieContainer();
I now have one CookieCollection that saves the session cookie, and another CookieCollection that saves two cookies to authenticate to the site. I then iterate through each CookieCollection, adding each cookie to the final request as such:

Expand|Select|Wrap|Line Numbers
  1.  
  2. foreach (oneCookie in myCollection)
  3. {
  4. request.CookieContainer.Add(oneCookie
  5. }
  6.  
I can view each cookie, but I cannot get the final response that I am looking for.
Jan 21 '09 #4

Plater
Expert 5K+
P: 7,872
When you packet watch, do they all show up in the transfer?
Jan 21 '09 #5

100+
P: 233
Going to have to read about packet sniffing, and then I'll get back to you.
Jan 21 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.