Results 1 to 4 of 4

Thread: [C#] Basic Neopets Login

  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)

  3. #2
    MikeyR's Avatar
    Joined
    Jan 2013
    Posts
    111
    Userbars
    7
    Thanks
    54
    Thanked
    50/37
    DL/UL
    103/0
    Mentioned
    18 times
    Time Online
    12d 6h N/A
    Avg. Time Online
    4m
    After the login passes true, everything in the curly braces would be whatever you want the program to do correct?

    Definitely digging the tutorial. Also enjoying that they seem to be coming out right as I decided I want to practice C# with neo programs.

    EDIT: Looking through the Login() code, I have questions about understanding:

    Code:
    Match cookieMatch = cookieRegex.Match(html);
    this.NeoCookie = cookieMatch.Groups[1].Value;
    return cookieMatch.Success;
    1. cookieMatch - string of the cookie data found after the POST request?
    2. I think I understand after some reading, but Groups[1] = neologin=[a-z0-9_]{3,20}%2B[a-z0-9]{40}

    And then if there were more brackets in the regex:
    (neologin=[a-z0-9_]{3,20}%2B[a-z0-9]{40}) (This would be Groups[2]) (and Groups[3] right?)
    3. And then cookieMatch.Success just returns bool if the cookie string was found?

    With incorrect login credentials it doesn't set a cookie, then the method returns false because there was no cookie set to find, correct?
    Last edited by MikeyR; 12-01-2013 at 09:29 PM.
    ok

  4. #3
    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
    After the login passes true, everything in the curly braces would be whatever you want the program to do correct?
    Yes

    Code:
    cookieRegex = new Regex(@"^Set-Cookie: (neologin=[a-z0-9_]{3,20}%2B[a-z0-9]{40})", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
    
    Options: case insensitive; ^ and $ match at line breaks
    
    Assert position at the beginning of a line (at beginning of the string or after a line break character) �^�
    Match the characters “Set-Cookie: ” literally �Set-Cookie: �
    Match the regular expression below and capture its match into backreference number 1 �(neologin=[a-z0-9_]{3,20}%2B[a-z0-9]{40})�
       Match the characters “neologin=” literally �neologin=�
       Match a single character present in the list below �[a-z0-9_]{3,20}�
          Between 3 and 20 times, as many times as possible, giving back as needed (greedy) �{3,20}�
          A character in the range between “a” and “z” �a-z�
          A character in the range between “0” and “9” �0-9�
          The character “_” �_�
       Match the characters “%2B” literally �%2B�
       Match a single character present in the list below �[a-z0-9]{40}�
          Exactly 40 times �{40}�
          A character in the range between “a” and “z” �a-z�
          A character in the range between “0” and “9” �0-9�
    
    
    Created with RegexBuddy
    Code:
    Match cookieMatch = cookieRegex.Match(html);//find the cookieRegex Match in (string) html
    this.NeoCookie = cookieMatch.Groups[1].Value; //set account's cookie to the matched value.
    return cookieMatch.Success; //return true if Success
    With incorrect login credentials, the regex won't match the html. Thus, cookieMatch.Groups[1].Value will equal "" and cookieMatch.Success will be false.

  5. #4
    MikeyR's Avatar
    Joined
    Jan 2013
    Posts
    111
    Userbars
    7
    Thanks
    54
    Thanked
    50/37
    DL/UL
    103/0
    Mentioned
    18 times
    Time Online
    12d 6h N/A
    Avg. Time Online
    4m
    Quote Originally Posted by Zachafer View Post
    With incorrect login credentials, the regex won't match the html. Thus, cookieMatch.Groups[1].Value will equal "" and cookieMatch.Success will be false.
    Yea I know a bit of regex from school this semester dealing with shell scripts. I pretty much followed it all so that's sick. Stoked that this random UNIX class that I had to take is totally worth while. Although shell scripts and regex are pretty good to know, not sure how much I'll use them in game programming.
    ok

Posting Permissions

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