•  greg_926
  • 54.8% (Neutral)
  • YAF Forumling Topic Starter

I have tried searching for this and can't seem to find it anywhere so I do apologise if this is a repost.

Could someone give me some indication on how to add users programatically to the forum with VB.NET? I have integrated the login and that works fine but I can't figure out how to add someone to the forum. What steps do I need to take?


  •  greg_926
  • 54.8% (Neutral)
  • YAF Forumling Topic Starter
Has anyone got any idea on how to do this?

Surely someone has used their own forum registration page to register someone to the forum. If anyone knows how to do this, please let me know.
Yaf uses the .net membership framework - so use that. System.Web.Security.Membership.CreateUser() will create a user in the membership framework and the first time that user visits the forum, yaf will sync itself.

not jsheLPer
  •  greg_926
  • 54.8% (Neutral)
  • YAF Forumling Topic Starter

Thanks for your post.

I couldn't get the following to work as you suggested because it always logged me into my website but not the forum. I have no idea why.

Dim memStatus As New MembershipCreateStatus
Membership.CreateUser(strUsername, strPassword, strEmail, "", "", True, memStatus)
Roles.AddUserToRole(strUsername, "registered")

But I added what I saw someone else has added to their code as follows:

Dim strHash As String = FormsAuthentication.HashPasswordForStoringInConfigFile(strPassword, "SHA1")
Dim objUserKey As Object = YAF.Providers.Profile.DB.GetProviderUserKey(strAppName, strUsername)
YAF.Providers.Membership.DB.CreateUser(strAppName, strUsername, strHash, "", 0, strEmail, "", "", True, objUserKey)
YAF.Providers.Roles.DB.AddUserToRole(strAppName, strUsername, "Registered")

The second lot of code above logs me into my site as well as the YAF forum and I can then see the new user added to the yaf_User table but the forum is empty for the user once logged in, same with the Active Discussions. If the user has been created by the forums register section then it shows everything fine.

I saw another post on here with this problem but no one responded.

Anyone else experiencing this or has any solution?
  •  greg_926
  • 54.8% (Neutral)
  • YAF Forumling Topic Starter
There HAS to be an answer to this.

Anyone who has added a new user through their code surely knows how it's done with the code examples I gave above.

Please help. I am completely lost where to go from here.
I don't have my codebase in front of me, but I'll try to help you off the top of my head because I've been similarly frustrated many times. This might not help directly but might put you on the right track.

YAF uses the Role Provider, but also has its own internal notion of Groups. Each Role is also a Group inside YAF, and there's two database tables - yaf_Group, and yaf_UserGroup. The former is a list of Groups (Roles), the latter is the relationship between yaf_User and yaf_Group to handle group (role) membership. This is seperate and distinct from the Role Provider itself.

When you call the Providers.Role.AddUserToRole, it does NOT add them to the yaf_UserGroup. That would have to be done as a seperate measure. Perhaps this is a flaw in the design, I cannot say.

The reverse is also true - if you call the DB method to associate a user into yaf_UserGroup (I forget the DB call offhand that does that), then it is NOT added into the Provider Role membership.

Different parts of YAF look at different things. The admin page for the user, for example, reads and saves UserGroup based off the Provider Roles - so if you had programmatically added them to a UserGroup without adding them to the Provider, that UserGroup setting is deleted immediately upon bringing up their admin page and clicking on the tab to even view the groups.

I'd wager that logging in and looking at the forums, however, doesn't consider the Role Provider memberships - only the YAF UserGroup membership. The registration process & admin pages flag both sources when making changes so that's why a page-registered user works fine, but if you're doing it programmatically, you need to keep this duality in mind.

  •  greg_926
  • 54.8% (Neutral)
  • YAF Forumling Topic Starter

Thank you so very much for the information. I appreciate it a lot.

So what is the answer to my problem? I understand what is going wrong now but I don't know how to fix it.

You said when I call the Providers.Role.AddUserToRole, it does NOT add them to the yaf_UserGroup. That would have to be done as a seperate measure. So what is the code to do this?
//Login to YAF
String password = FormsAuthentication.HashPasswordForStoringInConfigFile(Login1.Password, "md5");
Object userID = DB.user_login(1, Login1.UserName, password);

if (userID == DBNull.Value)
string strHash = FormsAuthentication.HashPasswordForStoringInConfigFile(Login1.Password, "md5");
object objUserKey = YAF.Providers.Profile.DB.GetProviderUserKey("xxxx", Login1.UserName);
YAF.Providers.Membership.DB.CreateUser("xxxx", Login1.UserName, strHash, "", 0, currentUser.Email1, "", "", true, objUserKey);
YAF.Providers.Roles.DB.AddUserToRole("xxxx", Login1.UserName, "Registered");
objUserKey = YAF.Providers.Profile.DB.GetProviderUserKey("xxxx", Login1.UserName);
userID = YAF.Classes.Data.DB.user_aspnet(1, Login1.UserName, currentUser.Email1, objUserKey, true);

foreach (string role in Roles.GetRolesForUser(Login1.UserName))
YAF.Classes.Data.DB.user_setrole(1, objUserKey, role);

string idName = string.Format("{0};{1};{2}", userID, 1, Login1.UserName);
FormsAuthentication.SetAuthCookie(idName, Login1.RememberMeSet);
  •  bbobb
  • 100% (Exalted)
  • YAF Developer
If you compile YAF from svn in debug mode, you'll see option Generate Test Data in Database section. It contains all the simple logic to add users to Registered group and Newbie Rank.
  •  JamesM
  • 50.6% (Neutral)
  • YAF Forumling

I'm busy integrating YAF into a site. Using Bonescan's suggestions I'm able to log in a user if they already exist and create a new user if they don't.

The problem I'm having is that yaf_user_aspnet creates a user with password '-' and next time they try and log in yaf_user_login doesn't recognize the user. How should I set the password?

I'm using version 1.9.3.


Anthony Smith
Hi. I am an Admin at 
We are using YAF 2.1.1 | YAF © 2003-2014, Yet Another Forum.NET
Maybe this feature was not available in older versions of YAF being that the question was asked 5 years ago.

How to simply create and new add users on a YAF Site.

Go to Admin area of your YAF Site and Click on Users and Roles. Then Click Users.
Coupon Codes - Bonus Codes » Administration » Users
Click to View Full Size 

Then at the Bottom, Click New User Button.
It should Navigate you to the Create New User Page.
Click to View Full Size 

About Us

The YAF.NET is an open source .NET forum project. YAF.NET is supported by an team of international developers who are build community by building community software.

Powered by Resharper Donate with PayPal button

Project Twitter Updates

Copyright © YetAnotherForum.NET & Ingo Herbote. All rights reserved