C # Login to WebRequest

Well, I tried this question yesterday, but I'm not sure that I gave enough information, I got an answer, but it did not work for me. Basically what I do, the user opens this application for Windows forms and logs in. Then they enter the text into the text box and start the click. At this point, the launch function makes a web request to the server for which a login is required (a login that is initially executed after they open the program. For some reason, it still does not see that the user is logged in when performing the second request although a cookie has also been added to the cookie container I'm not sure what I am doing wrong, but I will send my code so that you can help me.

This is the function that is executed to log in when the user enters the application.

private void button1_Click(object sender, EventArgs e) { string paramaters = "authmethod=on&chkRememberMe=on&login-form-type=pwd&password=" + pw.Text + "&userid=" + uid.Text + "&username=" + uid.Text; string strResponse; HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://www.url.com/login.form"); requestLogin.Method = "POST"; requestLogin.CookieContainer = cookieJar; requestLogin.ContentType = "application/x-www-form-urlencoded"; requestLogin.ContentLength = paramaters.Length; StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII); stOut.Write(paramaters); stOut.Close(); HttpWebResponse responseLogin = (HttpWebResponse)requestLogin.GetResponse(); StreamReader stIn = new StreamReader(responseLogin.GetResponseStream()); strResponse = stIn.ReadToEnd(); stIn.Close(); //Add cookies to CookieJar (Cookie Container) foreach (Cookie cookie in responseLogin.Cookies) { cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain)); richTextBox2.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString(); } if (strResponse.Contains("Log On Successful") || strResponse.Contains("already has a webseal session")) { foreach (Control cont in this.Controls) { cont.Visible = true; } loginPanel.SendToBack(); loginPanel.Visible = false; } else { MessageBox.Show("Login failed."); } } 

This is a function that starts when the user clicks the “run” button to initiate tests in the user account.

 private string runTestRequest(Uri url, string parameters) { string testResults = string.Empty; HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create(url); runTest.CookieContainer = cookieJar; runTest.Method = "POST"; runTest.ContentType = "application/x-www-form-urlencoded"; StreamWriter stOut = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII); stOut.Write(parameters); stOut.Close(); StreamReader stIn = new StreamReader(runTest.GetResponse().GetResponseStream()); testResults = stIn.ReadToEnd(); stIn.Close(); return testResults; } 

And of course this is my container cookie

 public CookieContainer cookieJar = new CookieContainer(); 

PS: Web request domains are different. The first of these is abc.com 2nd being 123.com. The only problem is that the first domain (which is the login) is the global login for internal web applications such as 123.com, so how can I use a login session from the 1st domain to the 2nd domain?

Could you help me figure out what I'm doing wrong.

+4
c # cookies winforms webrequest
source share
2 answers

I found out that what happened was that he was replaying with a subdomain in the same domain as the second (123.com) in order to use the login. Obviously, they had this global login system, built on several domains for the transmission of cookies. The code above works, and now I have work. Thanks!!

+1
source share
 string url = "http://www.ABC/MemberShip/Login.aspx";// HttpContext.Current.Request.Url.AbsoluteUri.ToString().Replace("AutoLogin", "Login"); CookieContainer myCookieContainer = new CookieContainer(); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = myCookieContainer; request.Method = "GET"; request.KeepAlive = false; HttpWebResponse response = request.GetResponse() as HttpWebResponse; System.IO.Stream responseStream = response.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); string srcString = reader.ReadToEnd(); // get the page ViewState string viewStateFlag = "id=\"__VIEWSTATE\" value=\""; int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length; int j = srcString.IndexOf("\"", i); string viewState = srcString.Substring(i, j - i); // get page EventValidation string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\""; i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length; j = srcString.IndexOf("\"", i); string eventValidation = srcString.Substring(i, j - i); string submitButton = "LoginButton"; // UserName and Password string userName = "userid"; string password = "password"; // Convert the text into the url encoding string viewState = System.Web.HttpUtility.UrlEncode(viewState); eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation); submitButton = System.Web.HttpUtility.UrlEncode(submitButton); // Concat the string data which will be submit string formatString = "txtUserName={0}&txtPassword={1}&btnSignIn={2}&__VIEWSTATE={3}&__EVENTVALIDATION={4}"; string postString = string.Format(formatString, userName, password, submitButton, viewState, eventValidation); // Convert the submit string data into the byte array byte[] postData = Encoding.ASCII.GetBytes(postString); // Set the request parameters request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.Referer = url; request.KeepAlive = false; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; CIBA)"; request.ContentType = "application/x-www-form-urlencoded"; request.CookieContainer = myCookieContainer; System.Net.Cookie ck = new System.Net.Cookie("TestCookie1", "Value of test cookie"); ck.Domain = request.RequestUri.Host; request.CookieContainer.Add(ck); request.CookieContainer.Add(response.Cookies); request.ContentLength = postData.Length; // Submit the request data System.IO.Stream outputStream = request.GetRequestStream(); request.AllowAutoRedirect = true; outputStream.Write(postData, 0, postData.Length); outputStream.Close(); // Get the return data response = request.GetResponse() as HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); srcString = reader.ReadToEnd(); Response.Write(srcString); Response.End(); 
+1
source share

All Articles