Results 1 to 4 of 4

Thread: [C#] Basic Neopets Login

Threaded View

  1. #1
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m

    [C#] Basic Neopets Login

    Hello. Today I will be showing you the start of nearly all Neo programs: the login!

    Let's get started. I'm assuming you have the tools necessary for all of this:


    First open up a new C# Windows Forms Application project:


    You will see an empty form. Let's make this our login form!
    Change the properties of your form:
    • Name - frmLogin
    • Text - Neopets Login


    Create a groupbox (grpLogin). Add 2 textboxes (txtUser, txtPass) and place 2 labels next to them that saying Username: and Password:. Now add a Button (btnLogin), change the text to Login.


    Now that we have our login form designed, we need to add the code that logins to Neo.
    So when the user clicks the Login button, we want to use the values in txtUser and txtPass to login to Neo. Very simple!

    Double click on btnLogin to open up the btnLogin_Click() event.
    Before we try to login to Neo, we should ensure that the user actually entered a username and password.
    Code:
                if (txtUser.TextLength < 3)
                {
                    MessageBox.Show("Please insert a username!", "Error");
                }
                else if (txtPass.TextLength == 0)
                {
                    MessageBox.Show("Please insert a password!", "Error");
                }
                else //if we made it here then txtUser.TextLength >= 3 && txtPass.textLength > 0
                { 
    
                }
    Here is where I'd like to introduce my C# Neo connection class, NeoAccount.cs. I will attach it to this thread, but it is also posted here: (you need an account to see links)

    Once you have NeoAccount.cs added to your Project, open up your browser and navigate to Neopets.com

    I'm using Firefox with LiveHTTPHeaders in this tutorial. It is possible to use a different browser with Fiddler but I prefer Firefox.

    Make sure the Capture checkbox is clicked in LiveHTTPHeaders and login to Neopets. You'll see the headers filling up fast. The first one is what we want (to login.phtml)


    Notice those 4 things I pointed out.

    The top line is the URL we are sending the request to.
    The second line tells you what type of request we are sending. (Most commonly GET or POST)
    The referer tells the website what page we are coming from. This can be spoofed easily.
    The data is the data we are POST-ing to the page. Content-Length and the data are sent in POST requests, not GET.

    Now we can use our NeoAccount.cs class to login to Neo.
    Code:
                    NeoFunctions.Account acc = new NeoFunctions.Account(txtUser.Text, txtPass.Text); //declare and initialize a new Account to connect to Neo
                    string data = "destination=%252Findex.phtml&username=" + txtUser.Text + "&password=" + txtPass.Text; //format our POST data string
                    string html = acc.Post("/login.phtml", data, "http://www.neopets.com/"); //send a POST request and store results in html
                    bool loggedIn = html.Contains("Set-Cookie: neologin=" + txtUser.Text.ToLower()); //look for the cookie to be set, thus we are logged in
                    if (loggedIn)
                    {
                        //here is where we load the main program's form
                    }
    But wait there is a very much easier way built into the NeoAccount class! To log in to Neo using the class's .Login() method:
    Code:
      NeoFunctions.Account acc = new NeoFunctions.Account(txtUser.Text, txtPass.Text); 
      if (acc.Login())
        {
    
        }
    This concludes my tutorial. I have attached the sample project files. If you have any questions, comments, or suggestions, feel free to post them below.
    Attached Files Attached Files

  2. The Following 2 Users Say Thank You to Zachafer For This Useful Post:

    j03 (12-01-2013),MikeyR (12-01-2013)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •