PDA

View Full Version : [C#] Basic Neopets Login



Zachafer
12-01-2013, 07:09 PM
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:

Web browser (Google Chrome ([Only registered and activated users can see links]) or Firefox ([Only registered and activated users can see links]) with Live[Only registered and activated users can see links] ([Only registered and activated users can see links]), I'm using FF)
Latest Visual Studio (I'm using 2012)


First open up a new C# Windows Forms Application project:
[Only registered and activated users can see links]

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.
[Only registered and activated users can see links]

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.

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: [Only registered and activated users can see links](includes-[Only registered and activated users can 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 Live[Only registered and activated users can see links] 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 Live[Only registered and activated users can see links] and login to Neopets. You'll see the headers filling up fast. The first one is what we want (to login.phtml)
[Only registered and activated users can see links]

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.

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, "[Only registered and activated users can see links]"); //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:

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.

MikeyR
12-01-2013, 08:35 PM
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:



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?

Zachafer
12-02-2013, 04:23 AM
After the login passes true, everything in the curly braces would be whatever you want the program to do correct?
Yes




[Only registered and activated users can see links]

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



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.

MikeyR
12-02-2013, 06:18 AM
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.