I want to post username and password to a login page of a remote website using asp.net and pass it, to access login-required pages on website. In the other words suppose there is a page on a website that i would to surf it an grab something from it ,but login is required before it . how to call that Login page and post username and password from the asp.net application to pass it.
Thanks in advance
The comment with passing them as the query string only works for GET
parameters. This works for POST
. As you can see I use the form entries username
, password
and login
(the button) but in your case it might be entirely different values. So use a tool like Fiddler to catch which values a normal login is sending. When I did something similar I had to use a cookie-aware webclient because the service used cookies for session values. The service you are trying to access could use a query-session string - it entirely depends on the service.
Another problem I ran into when doing this, was that I had to fetch a session id from the html page and sent it for logging it. I have not included the code for that here, but I have it if you need it :)
var client = new CookieAwareWebClient();
client.Encoding = Encoding.UTF8;
// Post values
var values = new NameValueCollection();
values.Add("username", someusername);
values.Add("password", somepassword);
values.Add("login", "Login"); //The button
// Logging in
client.UploadValues(loginPageUrl, values); // You may verify the result. It works with https :)
// Download some secret page
var html= client.DownloadString(someurl);
CookieAwareWebClient
public class CookieAwareWebClient : WebClient
{
private CookieContainer cookieContainer = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = cookieContainer;
}
return request;
}
}
try reading this -> http://www.terminally-incoherent.com/blog/2008/05/05/send-a-https-post-request-with-c/
if you use firefox - download "tamper-data" and "live http-headers" to see what information is passed to the login site
with live http-headers you can see which data is passed in the POST
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With