hopefully somebody here will be able to help... I have done a search on the forums for this, but haven't found anything relevant since 2004...
I'm using CS 2.1 (forums only to start with) and it will be hung off an existing site with its own CMS and membership system (written in ASP with Jscript).
What I'm wanting to do is not single sign on, but simply when a user registers with the main site I would like to fire off a call to CS to setup a forum user.
Please be gentle as my .NET skills are limited :P
I am looking to do the same thing, so I would be interested in hearing a good response.
Thanks,
Michael
You call the CommunityServer.Users.Create method, passing in a pre-populated CommunityServer.Components.User object, like so:
CommunityServer.Components.User u = new CommunityServer.Components.User();
u. Username = "Scott";
u. Password = "Password";
u.Email = "CommunityServer.Guy@ntlworld.com"
u.AccountStatus = UserAccountStatus.Approved; // IMPORTANT!!!
u.DateCreated = DateTime.Now;
CommunityServer.Users.CreateUser(u);
cheers,
this is exactly the type of info I was after! I'll try it out over the next few days
Regards,
Alastair
Could anyone clarify which pieces of the CS server are needed to make this work?
I currently am using .Component, .Controls, .SqlDataProvider, and .ASPNet20MemberRole .dll ' s as well as importing the communityserver.config. and connection string information from CS's web.config.
I was explicit with the namespaces in the code I gave you, the code should work "as-is".
No worries :-). I misunderstood what the original poster was asking so I didn't quite see how your solution fit.
At any rate, I created a brand new page on the CS site just to test this and I ran into an issue. I keep getting "System.NullReferenceException: Object reference not set to an instance of an object." when i call the create method.
This code: if (CommunityServer.Components.CSContext.Current.User.IsAnonymous) throw new Exception("Not logged in");
throws back the error, so it looks like I need to create an instance of User to actually create the NEW user. Is that correct? If so, how can I accomplish that programatically?
Hmm, if you're getting an error message here:
CommunityServer.Components.CSContext.Current.User.IsAnonymous
can you tell me what is NULL?
CSContext.Current
or
CSContext.Current.User?
Whenever you access CSContext.Current.User you should *always* get a user object back, the anonymous user by default...
I'm wondering if your install is corrupt.
Anyway the correct way to check if someone is logged in is:
if (Request.IsAuthenticated)
{
... you're logged in
}
And now for the $64,000 question . . .
From within the calling method, how can I get the integer UserID of the newly created user? The only thing passed back by the method is a CreateUserStatus value.
I have to pass the UserID back to another application. I have a custom class for doing this, but I need the value of the integer in order to update the other database. So, from within CreateUser.CreateUser_Click, I instantiate my custom class in a variable pwud and *try* to assign the new UserId to one of it's properties. Unfortunately, I'm not getting the integer anywhere:
pwud.CSUserID = csContext.User.UserID; // nope
pwud.CSUserID = csContext.UserID; // no luck here either
pwud.CSUserID = user.UserID; // no luck, but no surprise either
Any suggestions?
LATER EDIT --
Woops. Silly me (heh). The very first technique I tried does indeed work: it was the external value I was losing. Pay me no nevermind. Nothing to see here.
For future reference -- in case anyone else needs this -- CSContext.Current.User.UserID works fine.
Thanks for the post. Just to clarify one thing, if you want this to work "as is" with no imports, it needs to be as follows:
u.AccountStatus = CommunityServer.Components.UserAccountStatus.Approved; // IMPORTANT!!!
Thanks again for the post.
JT
I am using CS 2007 and am trying to add users through code. I have added Telligent.Components.dll as a reference. When I try to instantiate a user object as explained, Intellisense does not show "User" as an option in my list for the CommunityServer.Components namespace. Where is it?
Later:
I found it. These namespaces aren't part of "Telligent". "CommunityServer" is the top level.
CommunityServer.
As it tries to load the underlying MembershipUser, which is null/nothing when anonymous.
StringBuilder sb = new StringBuilder(); sb.AppendLine("DECLARE @UserID uniqueidentifier" sb.AppendLine("DECLARE @cs_UserID int" sb.AppendLine("DECLARE @Time DateTime" sb.AppendLine("SET @Time=getdate()" sb.AppendLine("SET @UserId = null" sb.AppendLine("EXEC [dbo].[aspnet_Membership_CreateUser] 'dev','" + user.Username.Replace("\'", string.Empty) + "','pa$$w0rd','','" + user.Email.Replace("\'", string.Empty) + "','Your Email?','" + user.Email.Replace("\'", string.Empty) + "',1, @Time, @Time, 1,0, @UserId OUTPUT" sb.AppendLine("EXEC [dbo].[cs_user_CreateUpdateDelete] @cs_UserID OUTPUT ,@UserID ,'" + user.Username.Replace("\'", string.Empty) + "' ,'" + user.Email.Replace("\'", string.Empty) + "' , null, null ,1 ,0 ,0 ,'' ,@Time ,1 ,0 ,0.0 ,0x0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,1000 ,0 ,0" sb.AppendLine("EXEC [dbo].[aspnet_UsersInRoles_AddUsersToRoles] 'dev','" + user.Username.Replace("\'", string.Empty) + "','RegisteredUsers',@Time"
StringBuilder sb = new StringBuilder(); sb.AppendLine("DECLARE @UserID uniqueidentifier" sb.AppendLine("DECLARE @cs_UserID int" sb.AppendLine("DECLARE @Time DateTime"
sb.AppendLine("SET @Time=getdate()" sb.AppendLine("SET @UserId = null" sb.AppendLine("EXEC [dbo].[aspnet_Membership_CreateUser] 'dev','" + user.Username.Replace("\'", string.Empty) + "','pa$$w0rd','','" + user.Email.Replace("\'", string.Empty) + "','Your Email?','" + user.Email.Replace("\'", string.Empty) + "',1, @Time, @Time, 1,0, @UserId OUTPUT"
sb.AppendLine("EXEC [dbo].[cs_user_CreateUpdateDelete] @cs_UserID OUTPUT ,@UserID ,'" + user.Username.Replace("\'", string.Empty) + "' ,'" + user.Email.Replace("\'", string.Empty) + "' , null, null ,1 ,0 ,0 ,'' ,@Time ,1 ,0 ,0.0 ,0x0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,1000 ,0 ,0" sb.AppendLine("EXEC [dbo].[aspnet_UsersInRoles_AddUsersToRoles] 'dev','" + user.Username.Replace("\'", string.Empty) + "','RegisteredUsers',@Time"
running that string builder via a C# code will add the user directly into the database. Crude I know, but it works
Nick - nb development
Will this also automatically log the user into CS the first time a user is created?
You can't do that via that code. You could add a page to the cs installation to look in your CMS system's database, get the username and password in a cookie, and sign the usering and or create it. Look at the SDK for more information on that, look for the way a user is logged in via the login form of community server
Copyright© 2008 Telligent Systems Inc. All rights reserved CommunityServer.com • Telligent.com