.Net → C# – Log in to Website Programmatically
In this tutorial, will show how to log into a website through code. I am going to use Twitter in my example.
THIS IS FOR EDUCATIONAL PURPOSES ONLY. I WOULD NOT ADVISE THE USE OF THIS TO ALWAYS LOG-IN TO TWITTER.
First, you will need to download and install Tamper Data. It’s an add-on for Firefox that allows you to view and tamper with GET/POST web request data. We won’t be doing any tampering. We will be using it to view what POST parameters the page is expecting.
Next, navigate to the log in page for Twitter(http://twitter.com/login). Once the page has loaded, go to Tools –> Tamper Data to open Tamper Data. At the top of Tamper Data, click the Start Tamper button. After clicking the button, click the “Sign In” button on Twitter’s log in page. Once you hit the Sign In button, Tamper Data will prompt you with this popup….

Click “Tamper”. You will then be presented with this window…

If you notice on the right-hand side of the window, you will see the POST parameters. These are:
authenticity_token
session[username_or_email]
session[password]
commit
Now that we have those, we can close the Tamper Data window, and close Firefox.
Now for the code. The code is actually fairly simple. We are just going to use the WebBrowser class to make the requests to the server to get the html source of the page. This will give us the “authenticity_token” for us to use in the POST request.
string url = "https://twitter.com/login";
string username = "someUserName";
string password = "somePassword";
string commit = "Sign+In"; //this matches the data from Tamper Data
private void Login()
{
WebBrowser b = new WebBrowser();
b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
b.Navigate(url);
}
private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser b = sender as WebBrowser;
string response = b.DocumentText;
// looks in the page source to find the authenticity token.
// could also use regular expressions here.
int index = response.IndexOf("authenticity_token");
int startIndex = index + 41;
string authenticityToken = response.Substring(startIndex, 40);
// unregisters the first event handler
// adds a second event handler
b.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted2);
// format our data that we are going to post to the server
// this will include our post parameters. They do not need to be in a specific
// order, as long as they are concatenated together using an ampersand ( & )
string postData = string.Format("authenticity_token={2}&session[username_or_email]={0}&session[password]={1}&commit={3}", username, password, authenticityToken, commit);
ASCIIEncoding enc = new ASCIIEncoding();
// we are encoding the postData to a byte array
b.Navigate("https://twitter.com/sessions", "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");
}
private void b_DocumentCompleted2(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser b = sender as WebBrowser;
string response = b.DocumentText;
if (response.Contains("Sign out"))
{
MessageBox.Show("Login Successful");
}
}
And that’s all you need to do. You can now use the response variable to see the tweets that are in your timeline.
Tags: C#, login, programmatically, WebBrowser, website
April 9th, 2010 at 6:05 AM
hi
is there any way to login to another website like orkut facebook etc..
any standard or general way ??
plzz help
April 12th, 2010 at 3:28 PM
since all sites use different post data, there is no general way. it has to be custom for the site that you want to login to.
April 21st, 2010 at 4:13 AM
Is there a way to do this using http request in C#?
simply using NetworkCredential(UserName, UserPwd)); does not work for me.
Is it possible to use the webBrowser to login and then pass on the cookies and stuff to httprequest so a login isnt necessary?
April 22nd, 2010 at 10:05 AM
hi, great tutorial, haven’t tried it yet, but still this is of great use. I’m trying to develop a mobile application for pixelpost, hope it works. Good luck !
May 14th, 2010 at 11:22 AM
@Mike,
I am not totally sure. What issues are you having? Are you behind a proxy?
May 19th, 2010 at 11:42 AM
Hi,
Once I’ve logged in, how would I store the cookie received and navigate to other secure pages?
And if not too much trouble, logout.
Thnx
May 20th, 2010 at 8:14 AM
@Lewi
The WebBrowser control should be storing the cookie itself. So as long as you keep that same instance of the WebBrowser, you should be able to navigate to any other secured page.
November 29th, 2010 at 3:03 PM
I created both a console app and a web app in VS.NET 2008 and tried to compile it. I get the following error on the lines where b_DocumentCompleted and b_DocumentCompleted2 are declared:
—————————————————
The type or namespace name ‘WebBrowserDocumentCompletedEventArgs’ could not be found (are you missing a using directive or an assembly reference?)
—————————————————
I looked for a System.WebBrowser reference/class and couldn’t find it. Is that what I am missing? Any help is appreciated!
November 29th, 2010 at 3:07 PM
Oh–it needs to be a Windows Forms application. I get it now!
December 20th, 2010 at 10:50 PM
Nice post . Thanks for, writing on this blog page mate. I shall email you again. I didn’t realise that!
December 28th, 2010 at 6:18 AM
Thanks for this nice pice of code Ryan. What assebleys are you using/declaring for this? Does System.WebBrowser suffice?
January 9th, 2011 at 8:11 PM
This site called Unblock Websites also has some decent information on the subject.
March 2nd, 2011 at 12:56 AM
THanks for the good post. However I am hitting road block login into this page.
http://d01.megashares.com/myms_login.php
Are you able to help me in getting the POST parameters?
I am not getting any errors but it’s just that the response string is not telling me anything even if I supply the wrong password and username. It;s just giving me a blank login screen.
I would appreciate your help.
Thanks
May 31st, 2011 at 11:39 PM
Wow, this post is really something, it makes me shiver, I think because of the topic
July 16th, 2011 at 6:22 PM
Generating free instant traffic to a blog is a critical element for the … by creating unique articles with keywords related to your site’s niche. Get free instant traffic to blog sites.
October 26th, 2011 at 9:24 AM
hello this was exactly the details I used to be seeking, does one brain basically discuss this using my viewers ?
November 4th, 2011 at 1:48 AM
We’re a gaggle of volunteers and starting a new scheme in our community. Your site offered us with useful info to paintings on. You have performed a formidable task and our entire neighborhood will probably be grateful to you.
November 19th, 2011 at 9:40 AM
We promote through various social media, including Facebook, Twitter, LinkedIn, and in the near future we will have a YouTube channel. In addition, we promote it off-line by word of mouth, business cards, and any other printed materials sent out to clients or potential clients.
November 21st, 2011 at 5:12 PM
Good job!! You really cleared out the last part of your post, wherein it is unclear and confusing
December 3rd, 2011 at 10:19 AM
Excellent post. I was checking constantly this blog and I am impressed! Very helpful information specially the last part
I care for such information a lot. I was looking for this particular information for a very long time. Thank you and best of luck.
December 7th, 2011 at 9:51 PM
Useful info is discussed and I am very thankful for the tips, excellent work you have there
January 31st, 2012 at 3:18 PM
Sorry, but I dispute with this unique upload. I do get pleasure from your personal page however and will likely continue to keep moving once more for posts.