I am trying to login to one of our internal websites to using a small programme using HttpWebRequest. You can find the below code I found in This Question to login to the website. Please find my changed code below according to our site.
class LoginSite
{
CookieContainer Cookies = new CookieContainer();
public void Main()
{
Login();
// Now the cookies in "Cookies" are all set.
// Ensure you set CookieContainer on all subsequent requests
}
void Login()
{
var wr = (HttpWebRequest)WebRequest.Create("http://portal.test.com/Login.aspx");
wr.Method = "POST";
wr.ContentType = "application/x-www-form-urlencoded";
wr.Referer = "http://portal.test.com/Login.aspx"; // my tests show this is needed
wr.CookieContainer = Cookies;
var parameters = new Dictionary<string, string>{
//{"__LASTFOCUS", ""},
//{"__EVENTTARGET",""},
// {"__EVENTARGUMENT",""},
//{"__VIEWSTATE","INdS1F45fzWYVwKD8b1b5x5Mr09nYrvS3hrSO4EAXFHVwtN76gSoToydwoOlIf1nI+KIxVR5CWiN2c5NOPO7kjTW7DbiNG2ZZ5Mpd0pYvN3tt68I6MvZ1dGHMJFhgJQ1YYpQQ+V6oonOrncJ6lwk69LaPVuSUSAPSubgSBoNYLfEy28kHirrNNc0uburWHQ9DK0zHwwod8gWKS8sz+qSjWOzV0PZyX7HhMQjc4MAD+RkHQMgeyOMqghODYn2ZW5mCBcY34ISL328aPGZmb6XERo+9LArRwASFBNqyFygZ1YJaUwReOdceEpTtg6TEOJb"},
//{"__VIEWSTATEGENERATOR", "A43BD702"},
//{"__EVENTVALIDATION","ngIch5+O80dcLktVDBbT/QqkZ8TvAm/HNMQVR54bGv0oQRxd2LqBISWhjdwgvwjnbaGSBAFDFcuvCgmdBvAJ/eBUSCNB9vMGfPzhecbdCFcGVzKy7/d49KNK/f8D+QBrT912wDav1/LFqFn32Op+HVOj2qLwuuvRLF+v4NL/xhpu22QNXXO/B4r/OAYJgtFYlZCg1k4jO7QacNdjaoJ02KVj0xBm9fWbr02spbCUld7+fY9shCyEcthLpEag8OryqKQlYtDgx8UxCc6bj5GKQw=="},
{"ctl00$cphBody$LogIn1$txtUserName", "testuser"},
{"ctl00$cphBody$LogIn1$vceUserName_ClientState", ""},
{"ctl00$cphBody$LogIn1$txtPassword", "testpwd"},
{"ctl00$cphBody$LogIn1$vecPassword_ClientState", ""},
{"ctl00$cphBody$LogIn1$btnSubmit", "Submit"}
};
using (var requestStream = wr.GetRequestStream())
using (var writer = new StreamWriter(requestStream, Encoding.UTF8))
writer.Write(ParamsToFormEncoded(parameters));
using (var response = (HttpWebResponse)wr.GetResponse())
{
// here you need to detect a correct login... this might be one of the cookies.
// if incorrect throw an exception or something.
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
var result = sr.ReadToEnd();
}
}
}
string ParamsToFormEncoded(Dictionary<string, string> parameters)
{
return string.Join("&", parameters.Select(kvp =>
Uri.EscapeDataString(kvp.Key).Replace("%20", "+") + "=" + Uri.EscapeDataString(kvp.Value).Replace("%20", "+")
).ToArray());
}
}
I have added the exact form values into parameters, I did not get any exception while getting a response, but still cookies count i am getting is zero. Can any one help me out please i have already spend lot of time on this. Below is the screen shot of Form Data.
I'm able to get a new cookie when uncommented LASTFocus,__EVENTTARGET,__EVENTARGUMENT,__VIEWSTATE,__VIEWSTATEGENERATOR,__EVENTVALIDATION and added wr.AllowAutoRedirect = false. But i did not get the new redirected page ie http://portal.test.com/Home.aspx in response. Instead i am getting the response like Object moved
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.