Add Users Programmatically - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Add Users Programmatically

  • Add Users Programmatically
  • 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
  • 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.
  • 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();

    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 >>
  • 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
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  • 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;

    Regards,
    Nicolas
  • only users with appropriate rights can create a user. 
    1.Replica Handbags And Shoes