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

Forums / General Discussions / Create user programmatically

Create user programmatically

15 posts, 0 answered
  1. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    13 Jan 2011
    Link to this post
    Hey all,

    I am trying to figure out how to add a user programmatically in sitefinity 4. At the moment, I have a user control with a asp:CreateUserWizard on it. Anyone who comes to the site can register. And hence, I've named the user control 'RegisterUserControl'.

    When creating the user, I have some code from an existing web application that would check if a specific role is not there, create if needed, and add the new user to the role.

    Here is the code I have so far:
    protected void CreateUserWizard_CreatingUser(object sender, LoginCancelEventArgs e)
    {
         TextBox firstNameTextbox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("FirstNameTextBox");
         TextBox lastNameTextbox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("LastNameTextBox");
         TextBox emailTextbox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
     
         if (Membership.GetUser(emailTextbox) == null)
         {
              string memberRole = ConfigurationManager.AppSettings["member_role"].ToString();
     
              CreateUserWizard cuw = (CreateUserWizard)sender;
              cuw.Email = cuw.UserName;
     
              if (!Roles.RoleExists(memberRole))
                   Roles.CreateRole(memberRole);
     
              if (!Roles.IsUserInRole(cuw.UserName, memberRole))
                   Roles.AddUserToRole(cuw.UserName, memberRole);
         }
    }

    On 'if (!Roles.RoleExists(memberRole))' I get:
    ProviderException was unhandled by user code.
    The Role Manager feature has not been enabled.

    I had created a previous thread about 'Role Manager feature has not been enabled?'. Here is the link.
    Now, in that case, I was able to get around the ProviderException by logging in as an admin user; however, in this case, I want the register page to be wide open so any one whom would come to the site can register.

    How do I change settings in sitefinity or how do I enable the role manager feature?

    Thanks,
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    13 Jan 2011
    Link to this post
    Hi Doug,

    You have to use RoleManager methods like RoleExists

    if (roleManager.RoleExists(role.Name))

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    13 Jan 2011
    Link to this post
    Thanks
  4. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    13 Jan 2011
    Link to this post
    I am now using roleManager to check if a particular role exists and what not. I also have moved some code around. Here is how it looks now:
    protected void CreateUserWizard_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        TextBox emailTextbox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
        if (Membership.GetUser(emailTextbox.Text) == null)
        {
            CreateUserWizard cuw = (CreateUserWizard)sender;
            cuw.Email = cuw.UserName;
        }
    }
     
    protected void CreateUserWizard_CreatedUser(object sender, EventArgs e)
    {
        string userName = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName")).Text;
        User user = (User)Membership.GetUser(userName);
     
        if (user != null)
        {
            string memberRole = ConfigurationManager.AppSettings["member_role"].ToString();
     
            var roleManager = RoleManager.GetManager();
     
            if (!roleManager.RoleExists(memberRole))
            {
                roleManager.CreateRole(memberRole);
            }
     
            Role role = roleManager.GetRole(memberRole);
     
            if (!roleManager.IsUserInRole(user.Id, memberRole))
            {
                roleManager.AddUserToRole(user, role);
            }
        }
    }

    At the moment, the one problem I'm having is that I get an Exception message stating that 'You are not authorized to 'Manage Users' ('Backend'). I am using a create user wizard to allow a person who comes to the site to be able to register.

    How do I change this to allow a new user to the site to be able to register? Do I do this in permissions somewhere?
  5. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    13 Jan 2011
    Link to this post
    I found manage roles and manage users in permissions and set to 'Everyone'. I think this solves it.
  6. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    13 Jan 2011
    Link to this post
    Actually that didn't seem to fix it.

    I went to Administration > Permissions and then changed Manage Users & Manage Roles under global permissions to 'Everyone' and I still get that 'You are not authorized to Manage Users ('Backend').

    Any thoughts?
  7. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    14 Jan 2011
    Link to this post
    Anyone got any ideas?
  8. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    14 Jan 2011
    Link to this post
    I've found this post.
  9. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    14 Jan 2011
    Link to this post
    I found new issue with adding a user programmatically. First through, in the CreatingUser delegate method, I added this line of code.
    UserManager.GetManager().Provider.SuppressSecurityChecks = true;

    So, including that line of code, I was able to create a user programmatically; however, the user doesn't get added to the role that I have specified. I went ahead and debugged and in CreatedUser delegate method, this code:
    Role role = roleManager.GetRole(memberRole);
    if (!roleManager.IsUserInRole(user.Id, memberRole))
    {
        roleManager.AddUserToRole(user, role);
    }

    Is the 'SuppressSecurityChecks' causing problems?

    This is what the full code looks like now:
    protected void CreateUserWizard_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        UserManager.GetManager().Provider.SuppressSecurityChecks = true;
     
        TextBox emailTextbox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
        if (Membership.GetUser(emailTextbox.Text) == null)
        {
            CreateUserWizard cuw = (CreateUserWizard)sender;
            cuw.Email = cuw.UserName;
        }
    }
     
    protected void CreateUserWizard_CreatedUser(object sender, EventArgs e)
    {
        string userName = ((TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName")).Text;
        User user = (User)Membership.GetUser(userName);
     
        if (user != null)
        {
            string memberRole = ConfigurationManager.AppSettings["member_role"].ToString();
     
            var roleManager = RoleManager.GetManager();
     
            if (!roleManager.RoleExists(memberRole))
            {
                roleManager.CreateRole(memberRole);
            }
     
            Role role = roleManager.GetRole(memberRole);
     
            if (!roleManager.IsUserInRole(user.Id, memberRole))
            {
                roleManager.AddUserToRole(user, role);
            }
        }
    }
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    17 Jan 2011
    Link to this post
    Hello Doug,

    Try calling RoleManager.SaveChages() after you assign a user to a role

    I prepared a small sample which works fine with the official release.

    var userManager = UserManager.GetManager();
    var user = userManager.GetUsers().Where(u => u.UserName == "test1").SingleOrDefault();
    var manager = RoleManager.GetManager("AppRoles");
    var role = manager.GetRoles().Where(r => r.Name == "Administrators").SingleOrDefault();
    manager.AddUserToRole(user, role);
    manager.SaveChanges();


    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    18 Jan 2011
    Link to this post
    What about custom roles not the ones provided?

    I've created several custom roles and can't seem to add users to those roles.
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Jan 2011
    Link to this post
    Hi Doug,

    The custom roles you created are part of the Default provider.


    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  13. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    18 Jan 2011
    Link to this post
    Do you just use an empty parenthesis with the role manager.

    RoleManager.GetManager();

    Do you do this to get all the custom roles?

  14. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Jan 2011
    Link to this post
    Hello Doug,

    GetManager() gets a manger instance for the default data provider which should contain your custom roles.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  15. Doug
    Doug avatar
    34 posts
    Registered:
    20 Sep 2010
    18 Jan 2011
    Link to this post
    Got it to work. Forgot to call save changes method on role manager.

    // forgot to call savechanges
    roleManager.SaveChanges();
15 posts, 0 answered