• (2 Pages)
  • +
  • 1
  • 2

C# - Log in to Website Programmatically How to log in to a website with code.

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 27 January 2010 - 10:22 AM

*
POPULAR

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….

Attached Image

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

Attached Image

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.

Is This A Good Question/Topic? 5
  • +

Replies To: C# - Log in to Website Programmatically

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 19 April 2010 - 04:50 AM

View PostGaurav, on 09 April 2010 - 05:04 AM, said:

how to sign in to another website.. any other like orkut etc.
is there any standard way


No, there is no standard way, since all websites use different post parameters.

View PostCarlos, on 24 March 2010 - 08:52 AM, said:

how use this example???


follow the directions?
Was This Post Helpful? 2
  • +
  • -

#3 Guest_pmajn*


Reputation:

Posted 03 May 2010 - 01:25 AM

thanks
Was This Post Helpful? 0

#4 Groboto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-September 10

Posted 26 September 2010 - 01:20 PM

HELLO!


Basically what i want do is from wpf or c# loging to wordpress, if login is succesfully show some message, and if login not succesfully show other message inside my software.
the adress in wordpress is http://www.webpage/wp-login.php
I not have idea how do this and i not are expert in c#
Best regards!
Was This Post Helpful? 0
  • +
  • -

#5 Guest_HelpingYou*


Reputation:

Posted 30 October 2010 - 06:49 AM

View PostGroboto, on 26 September 2010 - 12:20 PM, said:

HELLO!


Basically what i want do is from wpf or c# loging to wordpress, if login is succesfully show some message, and if login not succesfully show other message inside my software.
the adress in wordpress is http://www.webpage/wp-login.php
I not have idea how do this and i not are expert in c#
Best regards!


Although this guy's post is fairly old, in his example you just examine wordpresses theme. Like I did.
The only difference is the post parameters differ, and you don't need some info.

So the example would be like this :
 private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        WebBrowser b = sender as WebBrowser;

        string response = b.DocumentText;

        b.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);

        b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted2);

        string postData = string.Format("log={0}&pwd={1}&wp-submit={2}", username, password, commit);
                
        ASCIIEncoding enc = new ASCIIEncoding();


     b.Navigate("http://lazy-droid.com/wp-login.php", "", 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("Log Out"))
     {
         MessageBox.Show("Login Successful, have fun!", "Login Succeeded!");
         loggedin = true;

     }
     else
     {
         MessageBox.Show("All you need is an account(free) to use it.", "Login Failed!");
     }
}


Was This Post Helpful? 0

#6 Guest_Jure*


Reputation:

Posted 12 January 2011 - 03:13 PM

can you please help me login to this site?
data from tamper data:
username = test123456789
password = test123456789
return_page = http://http%3A%2F%2F...gin-to-ppm.html
input_page = en
submit = Log+into+the+world+of+PowerPlay+Manager%21
---
referer = http://ppm.powerplay...gin-to-ppm.html
php url = http://www.powerplay..._user_login.php
method = post

source code:

 <form action='http://www.powerplaymanager.com/action/action_ams_user_login.php' method='post'>

   <tr>

    <td colspan='2' align='center'>
     <div style='font-weight: bold; text-align: center; color: red;'>
  Log out successful
     </div>
 <br>
    </td>
   </tr>
  
 <tr>
  <td style='text-align:right;'>User name</td>

  <td><input type='text' name='username' size='20' class='registration' value='' /></td>
 </tr>
 <tr>
  <td style='text-align:right'>Password</td>
  <td><input type='password' name='password' size='20' class='registration' value='' /></td>
 <tr>
 <tr>
  <td colspan='2' align='center' style='border:0px;'>

 <br>
   <input type='hidden' name='return_page' value='http://www.powerplaymanager.com/ppm/en/login-to-ppm.html' />
   <input type='hidden' name='input_page' value='en' />
   <input type='submit' name='submit' value="Log into the world of PowerPlay Manager!" />
   
   <br /><br />
   
   <fb:login-button autologoutlink='false' v='2' size='long' onlogin='fbLoginCheck();' perms='email'>Log in via Facebook</fb:login-button>
  </td>
 <tr>



i am having trouble building up the postData string. Help would be very appritiated.

Thanks
Was This Post Helpful? 0

#7 jabcek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 13-January 11

Posted 13 January 2011 - 05:19 AM

Ohh, and facebook login is not neccery.

Thanks

Jure
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 17 January 2011 - 06:26 AM

this works...

string url = "http://www.powerplaymanager.com/action/action_ams_user_login.php";
string username = "test123456789";
string password = "test123456789";
string return_page = "http%3A%2F%2Fwww.powerplaymanager.com%2Fppm%2Fen%2Flogin-to-ppm.html";
string inputPage = "en";
string commit = "Log+into+the+world+of+PowerPlay+Manager%21"; //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;

    // 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("username={0}&password={1}&return_page={2}&input_page={3}&submit={4}", username, password, return_page, inputPage, commit);

    ASCIIEncoding enc = new ASCIIEncoding();

    //  we are encoding the postData to a byte array
    b.Navigate(url, "", 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(username))
    {
        MessageBox.Show("Login Successful");
    }
}


Was This Post Helpful? 0
  • +
  • -

#9 full_int  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 11

Posted 23 January 2011 - 11:08 AM

What to do when input dont have name? And Tamper don't show enything :(
<input type="submit" value="LogIN" />

Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 23 January 2011 - 02:49 PM

can you give the website?
Was This Post Helpful? 0
  • +
  • -

#11 full_int  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 11

Posted 23 January 2011 - 03:19 PM

I used Tamper and Web developer in firefox, and i can't find how to submit.
baltracker.net
It's private tracker, but i can create account for your testing, if you want.
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 24 January 2011 - 06:05 AM

I'm not sure what the problem is, as I can get TamperData to work just fine...

Attached Image

You make a POST request to "http://www.baltracker.net/login.php" with the POSTDATA of "uid=[username]&pwd=[password]". Seems to be pretty straightforward to me.
Was This Post Helpful? 1
  • +
  • -

#13 jabcek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 13-January 11

Posted 24 January 2011 - 06:22 AM

what about my problem, i am trying to send a message inside the site. Here is the code:
     <form action='http://www.powerplaymanager.com/action/action_send_mail.php' method='post'>
     <tr>
      <td class='ppm_body' valign='top' width='100%'>
    

        <div class='universal_form_element'>

         <strong>Prejemnik</strong>
         <input type='text' name='whom' size='30' value='' />
        </div>
    
        <div class='universal_form_element'>
         <strong>Zadeva</strong>
         <input type='text' name='subject' size='30' maxlength='50' value='' />
        </div>
    
        <div class='universal_form_element'>

         <strong>Besedilo</strong>
         <textarea name='text' rows='20' cols='100'></textarea>
        </div>
    
        <div>
         <input type='hidden' name='admin' value='0' />
         <input type='hidden' name='id_sort' value='1' />
         <input type='hidden' name='url_send_ok' value='http://ppm.powerplaymanager.com/sl/posta.html?data=unread' />
         <input type='hidden' name='url_send' value='http://ppm.powerplaymanager.com/sl/posta.html?data=send' />

         <input type='submit' value="Pošlji" />
        </div>

      </td>
     </tr>
     </form>



i tryed like this:

      string reciever= "testname";
            string subject= "Welcome";
            string text= "test123";
            string admin="0";
            string id_sort="1";
            string url_send_ok = "http%3A%2F%2Fppm.powerplaymanager.com%2Fsl%2Fposta.html%3Fdata%3Dunread";
            string url_send = "http%3A%2F%2Fppm.powerplaymanager.com%2Fsl%2Fposta.html%3Fdata%3Dsend";
            string submit="Pošlji";

            string postData1 = string.Format("whom={0}&subject={1}&text={2}&admin={3}&id_sort={4}&url_send_ok={5}&url_send={6}&submit={7}", reciever, subject, text, admin, id_sort,url_send_ok,url_send,submit);
            string url1="http://ppm.powerplaymanager.com/sl/posta.html?data=send";
            b.Navigate(url1, "", enc.GetBytes(postData1), "Content-Type: application/x-www-form-urlencoded\r\n");

            

But the only thing that is insered is reciever. All other fields are empty.
Was This Post Helpful? 0
  • +
  • -

#14 full_int  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 11

Posted 24 January 2011 - 08:07 AM

Thanks, it works now :) One more question, i get error when i try to login to filmai.in
Posted Image
private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
	 WebBrowser b = sender as WebBrowser;
	 //string response = b.DocumentText; //same error here

This post has been edited by full_int: 24 January 2011 - 08:08 AM

Was This Post Helpful? 0
  • +
  • -

#15 jabcek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 13-January 11

Posted 24 January 2011 - 08:30 AM

Post whole source code.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2