473,618 Members | 3,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form Login Help

233 New Member
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
5 3193
Plater
7,872 Recognized Expert Expert
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
mcfly1204
233 New Member
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
mcfly1204
233 New Member
So I have added the following to each HttpWebRequest:

Expand|Select|Wrap|Line Numbers
  1. request.CookieContainer = new CookieContainer();
I now have one CookieCollectio n that saves the session cookie, and another CookieCollectio n that saves two cookies to authenticate to the site. I then iterate through each CookieCollectio n, 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
7,872 Recognized Expert Expert
When you packet watch, do they all show up in the transfer?
Jan 21 '09 #5
mcfly1204
233 New Member
Going to have to read about packet sniffing, and then I'll get back to you.
Jan 21 '09 #6

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

Similar topics

9
8755
by: Dave Martin | last post by:
I've successfully used CURL to maneuver around and through sites but a new site I've been trying to work with has got me stumped. To retrieve the data I'm trying to get a login is required. If you go to any page in the "member" area without logging in you get directed to a https connection and hit login.php (ie: you end up at https://www.domain.com/login.php). The problem, I believe, lies in the fact that the form on that page...
4
1931
by: Fabio Negri Cicotti | last post by:
Hi. Imagine a scene where the user input the username and password in a "Login" form and when press the confirm button, another window is opened with the "Main" form. After that, the "Login" form is "closed" automatically. How can I do to close the "Login" form? If I use Login.Close() on the button_click event, both forms are closed.
9
1947
by: Justin Engelman | last post by:
Hi, I have a website that uses an ISAPI filter that will redirect anyone going to any page on the site to an SSL login page (on a different website with the same root domain) if they do not have the appropriate session cookie already. After logging in on that page successfully they are given the cookie and redirected back to the page they initially tried to go to. I would like to create a Windows application that gets data from that...
15
2277
by: carr4895 | last post by:
Hello. I was wondering if someone could help me too with a login form. Upon startup, I have to display a password screen and it should accept a user name and password. User name can be anything non-blank. If there is no user name, an appropriate error message must be displayed. All of the following passwords are valid IT160, VB2Manager, BackDoor. No other password is valid. It must allow the user to enter the password in any case. If...
10
1545
by: Partha Protim Roy | last post by:
Hello, My problem is: I got a Login form, so once the user enter vaild Username & Password another form opens. How do I close the login form?
14
3759
by: Simon Abolnar | last post by:
I would like to know how to open child form from dialog form. Thanks for help! Simon
5
2327
by: mouac01 | last post by:
I'm new to PHP/Javascript. I have a simple form I want to validate the fields with javascript and then run the PHP script. All the scripts are in one page. I want PHP to control where the next page is. It works fine without the javascript but when I add the javascript the page doesn't do anything besides the validation. Thanks for your help... Chong ----login.html-------------------------------- <?php ob_start();
5
17693
by: Navillus | last post by:
Hey gang, I have a login form that is empty by default, but can be filled with values from a previous form: <input type=text maxlength="40" size="40" name="user" value="`usr`"> <input type=password maxlength="8" name="password" value="`pss`"> where usr and pss are sent from the previous form.
4
3618
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if possible.. for example..i hav a menu in the parent form named "Administrator" whic has an item "mnuLogIn"..now when i click on login..another child form named "frmLogIn" is displayed..what i want to happen is this: when login form(frmLogIn) is...
13
3240
JodiPhillips
by: JodiPhillips | last post by:
G'day, I have a silly and simple problem that I need some guidance with. Due to the way our network is set up, I am unable to use the group permissions for Access and have had to implement log in procedures via a log in table within the database. This works fine. I have now expanded this table to include further data about the authorised user – Power User, Team Leader, & Facilitator. Depending on the user’s status as to which one of...
0
8212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8304
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8455
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5552
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4065
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.