Add Users Programmatically
Hi,
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();
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
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();
Hi,
The above method can we use in sitefinity 3.7.. or some modification needed for this ...?
Thanks,
Harish
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
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?
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?
Hi,
only users with appropriate rights can create a user. In code there is a way to avoid this :
XXXManager.Provider.SuppressSecurityChecks =
true;