5,660,782 members and growing! (148 online)
Email Password   helpLost your password?
Languages » C# » Utilities     Intermediate License: The Code Project Open License (CPOL)

Authentication to phpbb based forum using C#

By gyllbert

Authentication to phpbb based forum using C#
C# (C# 1.0, C# 2.0, C# 3.0, C#), Windows (Windows, Win2K, WinXP, Win2003, Vista), PHP

Posted: 5 Apr 2008
Updated: 5 Apr 2008
Views: 11,732
Bookmarked: 2 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
votes for this Article.
Popularity: 0.00 Rating: 0.00 out of 5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

One of my sites is a phpBB forum and I extended it recently with a new functionality. Users can download a small C# application and they can track posts by keywords. My users were pleased and I decided to add new features, like posting inside the application. The problem was to login the users so I will explain in this article how to create/keep/use the session.

Background

The most important part is how C# and phpBB manages cookies.

Using the code

First step is to prepare the post data which will be sent to forum, as a byte array. It will contain all information required by login process: username, password. A very important parameter is the one called "redirect", because the login.php script will redirect to main page when login succeeds. In this case no cookies will be received. The point is to specify an invalid redirect parameter so that phpBB will stop the execution(and so that cookies can be received)

// Note: autologin is set to keep the session for next "X" days (X is set in phpbb admin)
StringBuilder builder = new StringBuilder();
builder.Append("autologin=1&login=true&username=" + _user + "&password=" + _password + "&redirect=\n");

byte[] data = Encoding.ASCII.GetBytes(builder.ToString());
         
Next, get the cookies sent by phpBB:
string[] keys = response.Headers.GetValues("Set-Cookie");>  

The cookies should contain these two parameters: phpbb2mysql_data which is a serialized array containing user id and other information; phpbb2mysql_sid which is the session key. If the username and password are not corect, the phpbb2mysql_data will contain a -1 which indicates that login failed.

If login succeeds, these information like phpbb2mysql_sid,phpbb2mysql_data and user are stored in a file by using the serialization. The tricky part is that these information changes over time. During next request to the forum the phpBB will check your session in database and update it. This means that at every request sent, we need to update the cookies using the latest ones.

How to initialise the class:

PhpBBLogin.Instance.Domain = "www.your-domain.com";
PhpBBLogin.Instance.ValidityDaysForKey = 5;//set this value from your admin area
PhpBBLogin.Instance.LoadCache();
PhpBBLogin.Instance.OnError += new EventHandler(Instance_OnError); //if something goes wrong, use LastError public member
PhpBBLogin.Instance.OnLoginFinish += new EventHandler(Instance_OnLoginFinish);
PhpBBLogin.Instance.OnLogoutFinish += new EventHandler(Instance_OnLogoutFinish);
 

How to login:

PhpBBLogin.Instance.Login("username","password");
//This request is done in a separated thread. when it finishes the OnLoginFinish event is triggered
//and you can check the property PhpBBLogin.Instance.IsLogged to see if login succeed.

Creating new post when user is logged:

string message="new post";
string subject="subject";
int f=1; //this is the forum ID.
string post="Post";      
PhpBBLogin.Instance.PostMessage(
                    "subject=" + subject + "&" +
                    "message=" + message + "&" +                     
                    "topictype=" + topictype + "&" +                    
                    "f=" + f + "&" +
                    "post=" + post+"&"+
                    "mode=newtopic");



History

Version 1.0 submitted on 5.4.2008.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

gyllbert



Location: Romania Romania

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralvbulletinmemberPestinha198415:52 17 Aug '08  
QuestionAny simple complete example of calling the phpBB login?membera2001dummy12:00 15 Aug '08  
GeneralLogin credentialsmembernmilioti23:54 8 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Apr 2008
Editor:
Copyright 2008 by gyllbert
Everything else Copyright © CodeProject, 1999-2008
Mail5 | Advertise on the Code Project