Add Users Programmatically

Posted by Community Admin on 05-Aug-2018 10:59

Add Users Programmatically

All Replies

Posted by Community Admin on 15-Sep-2010 00:00

Hi,

is there an API to add a backend user from a custom control? And further, is it possible to extend sitefinity's security system and add custom fields to the user records?
Thanks for your Response,
Simon

Posted by Community Admin on 15-Sep-2010 00:00

Hello Simon,

Yes, it is possible to add a backend user through the API. Here is a sample code

var userManager = UserManager.GetManager("Default");
System.Web.Security.MembershipCreateStatus status;
var user = userManager.CreateUser("username", "username@password", "email@test.com", "Question1", "Answer1", true, null, out status);
user.FirstName = "First";
user.LastName = "Last";
user.Comment = "some comment here";
user.IsBackendUser = true;
userManager.SaveChanges();

There is API for managing profile data, but it is not finished yet and there could be some issues. You could refer to this post.

Best wishes,
Ivan Dimitrov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.

Posted by Community Admin on 20-Sep-2011 00:00

Hi ,

I was searching for the same problem as above . but difference is i am using some meta fields in user registration form and now i want to pass values to the API to add users dynamically with their roles... can you please tell me how can i pass the values for the meta fields and how to set roles. Roles are already created.

Thanks
Harish

Posted by Community Admin on 21-Sep-2011 00:00

Hi Harish,

Here is a full example:

var userManager = UserManager.GetManager("Default");
System.Web.Security.MembershipCreateStatus status;
var user = userManager.CreateUser("username", "username@password", "email@test.com", "Question1", "Answer1", true, null, out status);
user.IsBackendUser = true;
userManager.SaveChanges();
 
var profileManager = UserProfileManager.GetManager();
var profile = profileManager.CreateProfile(user, typeof(SitefinityProfile).FullName) as SitefinityProfile;
profile.FirstName = "First";
profile.LastName = "Last";
profileManager.SaveChanges();
 
var roleManager = RoleManager.GetManager();
var editorRole = roleManager.GetRole("Editor");
roleManager.AddUserToRole(user, editorRole);
roleManager.SaveChanges();

You can use the SitefinityProfile type (or Basic profile as it is named under Administration -> Users -> Manage Profile types) to add additional custom fields. Here you see that I set FirstName and LastName. The last bit of code assigns the Editor role to the newly created user.

Greetings,
Lubomir Velkov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Posted by Community Admin on 22-Sep-2011 00:00

Hi,

The above method can we use in sitefinity 3.7.. or some modification needed for this ...?

Thanks,
Harish

Posted by Community Admin on 22-Sep-2011 00:00

Hello Harish,

In 3.x you can use the standard ASP.NET classes for working with users and profiles.

Regards,
Ivan Dimitrov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Posted by Community Admin on 01-Aug-2012 00:00

I've tried the above code (I'm using Sitefinity 5.1, but it seems to work about the same.)  However, I am getting this error: "You are not authorized to 'Manage Users' ('Backend')."  Is there some kind of username/password that the program will need to pass in to get this to work?

Posted by Community Admin on 01-Aug-2012 00:00

I've tried the above code (I'm using Sitefinity 5.1, but it seems to work about the same.)  However, I am getting this error: "You are not authorized to 'Manage Users' ('Backend')."  Is there some kind of username/password that the program will need to pass in to get this to work?

Posted by Community Admin on 03-Aug-2012 00:00

Hi,

only users with appropriate rights can create a user. In code there is a way to avoid this :

XXXManager.Provider.SuppressSecurityChecks = true;

Regards,
Nicolas

Posted by Community Admin on 13-Sep-2012 00:00

only users with appropriate rights can create a user. 

This thread is closed