+1-888-365-2779
Try Now
More in this section

Forums / Sitefinity SDK / Add Users Programmatically

Add Users Programmatically

10 posts, 0 answered
  1. Simon
    Simon avatar
    1 posts
    Registered:
    08 Sep 2010
    15 Sep 2010
    Link to this post
    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
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    15 Sep 2010
    Link to this post
    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.
  3. Hari
    Hari avatar
    9 posts
    Registered:
    18 May 2011
    20 Sep 2011
    Link to this post
    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
  4. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    21 Sep 2011
    Link to this post
    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 >>

  5. Hari
    Hari avatar
    9 posts
    Registered:
    18 May 2011
    22 Sep 2011
    Link to this post
    Hi,

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

    Thanks,
    Harish
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    22 Sep 2011
    Link to this post
    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 >>

  7. Greg
    Greg avatar
    16 posts
    Registered:
    11 May 2011
    01 Aug 2012
    Link to this post
    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?
  8. Greg
    Greg avatar
    16 posts
    Registered:
    11 May 2011
    01 Aug 2012
    Link to this post
    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?
  9. Nicolas
    Nicolas avatar
    156 posts
    Registered:
    19 Jan 2011
    03 Aug 2012
    Link to this post
    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
  10. Huchen
    Huchen avatar
    3 posts
    Registered:
    13 Sep 2012
    13 Sep 2012
    Link to this post
    only users with appropriate rights can create a user. 
    1.Replica Handbags And Shoes
10 posts, 0 answered