Calling the create new users routines from another app

rated by 0 users
This post has 14 Replies | 6 Followers

Not Ranked
Posts 2
Points 55
ajhbell Posted: Fri, Aug 18 2006 11:12 AM

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

 

Top 500 Contributor
Posts 69
Points 1,245

I am looking to do the same thing, so I would be interested in hearing a good response.

Thanks,

Michael

Michael S. Scherotter Solution Platform Project Manager Mindjet Corporation
  • | Post Points: 5
Top 500 Contributor
Posts 88
Points 1,695

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

Regards, CommunityServer guy Available for hire for small jobs!!!!!
  • | Post Points: 50
Not Ranked
Posts 2
Points 55
ajhbell replied on Mon, Sep 11 2006 4:39 AM

cheers,

 this is exactly the type of info I was after! I'll try it out over the next few days Smile

 
Regards,

Alastair

 

  • | Post Points: 20
Not Ranked
Posts 2
Points 55
MuggleX replied on Wed, Sep 27 2006 3:48 PM

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.

 
 

  • | Post Points: 20
Top 500 Contributor
Posts 88
Points 1,695

I was explicit with the namespaces in the code I gave you, the code should work "as-is".

 

Edit: oops, looks like I was being condescending there- I can assure you that's not the case Stick out tongue
Regards, CommunityServer guy Available for hire for small jobs!!!!!
  • | Post Points: 20
Not Ranked
Posts 2
Points 55
MuggleX replied on Thu, Sep 28 2006 2:24 PM

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?

 

  • | Post Points: 35
Top 500 Contributor
Posts 88
Points 1,695

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 

 

 

Regards, CommunityServer guy Available for hire for small jobs!!!!!
  • | Post Points: 20
Top 75 Contributor
Posts 282
Points 2,710

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. Embarrassed


For future reference -- in case anyone else needs this -- CSContext.Current.User.UserID works fine. 

Ole Yearian Application Developer Cord Blood Registry
  • | Post Points: 5
Not Ranked
Posts 1
Points 5

 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:

 

CommunityServer.Components.User u = new CommunityServer.Components.User();

u. Username = "Scott";

u. Password = "Password";

u.Email = "CommunityServer.Guy@ntlworld.com"

u.AccountStatus = CommunityServer.Components.UserAccountStatus.Approved;            // IMPORTANT!!!

u.DateCreated = DateTime.Now;

CommunityServer.Users.CreateUser(u);

 

Thanks again for the post.

 

JT
 

Not Ranked
Posts 6
Points 60

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.

  • | Post Points: 5
Not Ranked
Posts 1
Points 20
Something I noted - if you dont set the IsAnonymous property to false, you will get an exception thrown when you call

CommunityServer.Users.Create()

As it tries to load the underlying MembershipUser, which is null/nothing when anonymous.

  • | Post Points: 20
Top 10 Contributor
Posts 2,423
Points 19,635
MVPs
Nick replied on Tue, Nov 20 2007 5:01 PM

 

                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("DECLARE @UserID uniqueidentifier"Wink;
                            sb.AppendLine("DECLARE @cs_UserID int"Wink;
                            sb.AppendLine("DECLARE @Time DateTime"Wink;

                            sb.AppendLine("SET @Time=getdate()"Wink;
                            sb.AppendLine("SET @UserId = null"Wink;
                            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"Wink;

                            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"Wink;
                            sb.AppendLine("EXEC    [dbo].[aspnet_UsersInRoles_AddUsersToRoles]    'dev','" + user.Username.Replace("\'", string.Empty) + "','RegisteredUsers',@Time"Wink;

running that string builder via a C# code will add the user directly into the database.  Crude I know, but it works 

  • | Post Points: 20
Not Ranked
Posts 14
Points 160
mavened replied on Wed, Nov 21 2007 8:01 PM

Will this also automatically log the user into CS the first time a user is created?

  • | Post Points: 20
Top 10 Contributor
Posts 2,423
Points 19,635
MVPs
Nick replied on Thu, Nov 22 2007 10:50 AM

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 

  • | Post Points: 5
Page 1 of 1 (15 items) | RSS
Powered by Community Server (Commercial Edition), by Telligent Systems

Copyright© 2008 Telligent Systems Inc. All rights reserved
CommunityServer.com  •  Telligent.com