Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[C#] vBulletin Login function

streamwriter login

  • Please log in to reply
12 replies to this topic

#1 TriggerHappy

TriggerHappy

    CC Regular

  • Just Joined
  • PipPipPip
  • 30 posts

Posted 15 February 2010 - 10:59 AM

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.
  • 0

#2 TriggerHappy

TriggerHappy

    CC Regular

  • Just Joined
  • PipPipPip
  • 30 posts

Posted 16 February 2010 - 11:59 AM

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

#3 Yustme

Yustme

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 01 March 2010 - 01:12 PM

Hi,

The code is very usefull!!!

Thanks man.

I do have small question.

Some where in your post you say:

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?
  • 0

#4 TriggerHappy

TriggerHappy

    CC Regular

  • Just Joined
  • PipPipPip
  • 30 posts

Posted 01 March 2010 - 09:14 PM

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);

  • 0

#5 Yustme

Yustme

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 02 March 2010 - 03:27 AM

Hi,

Thanks for your reply!

I get this error:

system.net.webexception: the operation has timed out

Shouldn't "HttpWebResponse c"

Be closed?
  • 0

#6 Yustme

Yustme

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 02 March 2010 - 10:09 AM

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?
  • 0

#7 TriggerHappy

TriggerHappy

    CC Regular

  • Just Joined
  • PipPipPip
  • 30 posts

Posted 02 March 2010 - 11:08 AM

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.
  • 0

#8 Yustme

Yustme

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 02 March 2010 - 12:50 PM

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?
  • 0

#9 Mighty Goober

Mighty Goober

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 05 August 2010 - 06:43 PM

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:

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";
  • 0

#10 saXen

saXen

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 19 November 2010 - 08:39 AM

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.
  • 0

#11 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 20 November 2010 - 03:18 AM

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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#12 aassddff

aassddff

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 05 September 2011 - 11:14 PM

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?
  • 0





Also tagged with one or more of these keywords: streamwriter, login

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download