hmillington
2011-03-07T14:32:31Z
This topic has been discussed here before, and it looks as if many people are trying to do it without success (or without a working solution being posted here, at least), but do accept my apolgies if I've just been unable to find it.

I want to create user accounts programatically from another database of users. I've seen some snippets of code here that look like they might form the basis of doing it but nothing that provides a complete solution.

My complete test code looks like that below, which is based on code posted here: http://forum.yetanotherf...ser-Programatically.aspx .

However it doesn't compile as the lines containing a YAF reference claim 'reference to a non-shared member requires a object reference' and I know how I'd usually solve that problem in my own code, but I don't know enough about YAF or membership providers to do so here.

I'm proposing to run the code in the same appolciation at YAF, so the library code is available to it.

What do I need to add? And also, what does the "XXXX" refer to?

Thanks

Huw

<%@ Page Language="VB" %>

<script runat="server">

Sub page_load()

Dim u As String = "test"
Dim pw As String = "password"
Dim e As String = "test@test.com"

Dim password As String = FormsAuthentication.HashPasswordForStoringInConfigFile(pw, "md5")
Dim userID As Object = DB.user_login(1, u, password)

If IsDBNull(userID) Then
Dim strHash As String = FormsAuthentication.HashPasswordForStoringInConfigFile(pw, "md5")
Dim objUserKey As Object = YAF.Providers.Profile.DB.GetProviderUserKey("xxxx", u)
YAF.Providers.Membership.DB.CreateUser("xxxx", u, strHash, "", 0, e, "", "", True, objUserKey)
YAF.Providers.Roles.DB.AddUserToRole("xxxx", u, "Registered")
objUserKey = YAF.Providers.Profile.DB.GetProviderUserKey("xxxx", u)
userID = YAF.Classes.Data.DB.user_aspnet(1, u, e, objUserKey, True)

For Each role As String In Roles.GetRolesForUser(u)
YAF.Classes.Data.DB.user_setrole(1, objUserKey, role)
Next
End If


Dim idName As String = String.Format("{0};{1};{2}", userID, 1, u)
FormsAuthentication.SetAuthCookie(idName, 1)
End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>
Sponsor
Tuff
  •  Tuff
  • 52.4% (Neutral)
  • YAF Forumling
2011-03-23T20:07:49Z
I normally code in C# and you'll need to add the namespace:
using YAF.Providers;

I believe in VB, you need to do it as follows:
Imports YAF.Providers

As for the "XXXX", that is the AppName. Look in your yaf_prov_Application table and see what the ApplicationName is. The default is "YetAnotherForum".

Hope this helps.
Steve4
2011-12-15T23:18:29Z
I'm trying to do the same thing and I can't get this code to compile. I copied the code above and converted it to C#. Some of the error message said that I needed a static reference to call some of the methods so I created objects like oRolesDB to call the static methods. Currently the error message is saying:

The type or namespace name 'DB' does not exist in the namespace 'YAF.Classes.Data'

Can anybody tell me what class or reference I need to complete this method? I'm also not sure why he's calling the foreach method because it looks like he's setting roles for the user that have already been set. Here's my broken code:

using System;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using YAF;
using YAF.Classes;
using YAF.Providers;
using YAF.Providers.Profile;

namespace YAF
{
public partial class LoadUsers : Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
private void CreateUser(String sApplicationName, String sUserName, String sPassword, String sEmail)
{
YAF.Classes.Data.DB oDataDB = new YAF.Classes.Data.DB();
YAF.Providers.Roles.DB oRolesDB = new YAF.Providers.Roles.DB();
YAF.Providers.Profile.DB oProfileDB = new YAF.Providers.Profile.DB();
YAF.Providers.Membership.DB oMembershipDB = new YAF.Providers.Membership.DB();
String sPasswordHashed = FormsAuthentication.HashPasswordForStoringInConfigFile(sPassword, "md5");
Object oUserKey = oProfileDB.GetProviderUserKey(sApplicationName, sUserName);
oRolesDB.AddUserToRole(sApplicationName, sUserName, "Registered");
oMembershipDB.CreateUser(sApplicationName, sUserName, sPasswordHashed, "", 0, sEmail, "", "", true, oUserKey);
String[] sUserRoles = Roles.GetRolesForUser(sUserName);
foreach (String sUserRole in sUserRoles)
{
oDataDB.user_setrole(1, oUserKey, sUserRole);
}
Object oUserLogin = oDataDB.user_aspnet(1, sUserName, sEmail, oUserKey, true);
String sUserIDFormatted = String.Format("{0};{1};{2}", oUserLogin, 1, sUserName);
FormsAuthentication.SetAuthCookie(sUserIDFormatted, true);
}
}
}
Steve4
2011-12-16T00:02:43Z
After doing a little more research it looks like the methods above are in the LegacyDB class. But when I try to reference that class I get the error: The type or namespace name 'LegacyDB' does not exist in the namespace 'YAF.Classes.Data'. But it does. Why doesn't this work?

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