Jump to content

[C#] vBulletin Login function

- - - - -

  • Please log in to reply
12 replies to this topic

#1
TriggerHappy

TriggerHappy

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Well, in one of my programs it was necessary to login to vBulletin (and stay logged in) so I wrote a function which does that.

Requires: System.Net; & System.IO;

        static string login(string url, string username, string password)
        {
            HttpWebRequest req  = (HttpWebRequest)WebRequest.Create(url);
            string cookie       = "";
            string values       = "vb_login_username="+username+"&vb_login_password="+password
                                + "securitytoken=guest&"
                                + "cookieuser=checked&"
                                + "do=login";
            req.Method          = "POST";
            req.ContentType     = "application/x-www-form-urlencoded";
            req.ContentLength   = values.Length;
            CookieContainer a   = new CookieContainer();
            req.CookieContainer = a;

            System.Net.ServicePointManager.Expect100Continue = false; // prevents 417 error

            using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII)) {writer.Write(values);}

            HttpWebResponse c = (HttpWebResponse)req.GetResponse();
            foreach (Cookie cook in c.Cookies){cookie = cookie + cook.ToString() + ";";}

            return cookie;
        }
        

It returns the cookie data so when loading other pages of the site, you can just send the cookies and stay logged in with the headers.add("cookie", cookie) method.

And an example;

    string cookie = login("forum.codecall.net/login.php?do=login", "username", "password"); // include http and www (I don't have 10 forum posts)
     Console.Write(cookie);
     Console.Read();

This has only been tested on vBulletin 3.x versions.

#2
TriggerHappy

TriggerHappy

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Bump.
Nobody finds this interesting? You can login to codecall (along with other vBulletin powered sites) with it. I thought somebody would like it =(

#3
Yustme

Yustme

    Newbie

  • Members
  • Pip
  • 4 posts
Hi,

The code is very usefull!!!

Thanks man.

I do have small question.

Some where in your post you say:

Quote

It returns the cookie data so when loading other pages of the site, you can just send the cookies and stay logged in with the headers.add("cookie", cookie) method.

Where do I find this 'headers' method?

#4
TriggerHappy

TriggerHappy

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
It's in the WebClient class.

WebClient client = new WebClient();
string cookie    = login("http://www.forum.codecall.net/login.php?do=login", "username", "password");
client.Headers.Add("Cookie", cookie);


#5
Yustme

Yustme

    Newbie

  • Members
  • Pip
  • 4 posts
Hi,

Thanks for your reply!

I get this error:

system.net.webexception: the operation has timed out

Shouldn't "HttpWebResponse c"

Be closed?

#6
Yustme

Yustme

    Newbie

  • Members
  • Pip
  • 4 posts
Hi,

Somehow, it doesn't log me in.

form action="login.php?do=login" method="post" onsubmit="md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0)">

Shouldn't the password be hashed first?

#7
TriggerHappy

TriggerHappy

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
It doesn't need to be hashed, I've tested logging in on several languages with no hash and it worked.
I've also tested this piece of code and it worked so I'm not sure what your problem is.

#8
Yustme

Yustme

    Newbie

  • Members
  • Pip
  • 4 posts
Just trying to figure out why its not working...

string values = "vb_login_username="+username+"&vb_login_password="+password + "securitytoken=guest&"


Between 'password' and 'securitytoken' shouldn't there be an ampersand too?

#9
Mighty Goober

Mighty Goober

    Newbie

  • Members
  • Pip
  • 1 posts
I am having a few troubles, I registered here just to ask them and get them solved.

Line:
HttpWebResponse c = (HttpWebResponse)req.GetResponse();

Exception:
The remote server returned an error: (503) Server Unavailable.

I have a forum hosted at:
Goobers - Powered by JosheR

Does that make the url variable:
string cookie = login("http://www.goobs-serv.com/forums/login.php?do=login", "username", "password");



EDIT:

Yustme said:

Just trying to figure out why its not working...

string values = "vb_login_username="+username+"&vb_login_password="+password + "securitytoken=guest&"


Between 'password' and 'securitytoken' shouldn't there be an ampersand too?
This worked, thanks!

string values = "vb_login_username=" + username +
"&vb_login_password=" + password +
"&securitytoken=guest" +
"&cookieuser=checked" +
"&do=login";

#10
saXen

saXen

    Newbie

  • Members
  • Pip
  • 1 posts
Hi. I'm sorry, but I just don't manage to use this code as I intend. I have the username and password of the forum user. What I want is by a button in my C# .net websolution make the user logged in to the vBulletinforum, and redirect the user to the forum mainpage where he now is logged in.

When running the following
string cookie = login("forumurl/login.php?do=login", "username", "password");
string cookie ends up containing:
bbsessionhash=c31332c3ec8128ab2d03840e90ecfac9;bblastvisit=1290184204;bblastactivity=0;

Now. How do I use this for becomeing logged in to my forum?
When redirecting the forum, nothing happens.
What does Console.Write(cookie);Console.Read(); do?
How do I use the cookie string to make my user become automatically logged in when entering the forum.

Please help. I would really appreciate it.

#11
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

saXen said:

Now. How do I use this for becomeing logged in to my forum?
When you log in, you are assigned a session cookie to initiate each session while logged in. You have to send that cookie to the site each time you wish to view it while logged in.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#12
aassddff

aassddff

    Newbie

  • Members
  • Pip
  • 1 posts
Can anyone tell me how to use this code when I have a website in other than UTF-8 encoding? When I use code posted here I lose my national characters. Does anyone know how to avoid it?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users