Create user programmatically

Posted by Community Admin on 03-Aug-2018 21:39

Create user programmatically

All Replies

Posted by Community Admin on 13-Jan-2011 00:00

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,

Posted by Community Admin on 13-Jan-2011 00:00

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

Posted by Community Admin on 13-Jan-2011 00:00

Thanks

Posted by Community Admin on 13-Jan-2011 00:00

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?

Posted by Community Admin on 13-Jan-2011 00:00

I found manage roles and manage users in permissions and set to 'Everyone'. I think this solves it.

Posted by Community Admin on 13-Jan-2011 00:00

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?

Posted by Community Admin on 14-Jan-2011 00:00

Anyone got any ideas?

Posted by Community Admin on 14-Jan-2011 00:00

I've found this post.

Posted by Community Admin on 14-Jan-2011 00:00

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);
        
    

Posted by Community Admin on 17-Jan-2011 00:00

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

Posted by Community Admin on 18-Jan-2011 00:00

What about custom roles not the ones provided?

I've created several custom roles and can't seem to add users to those roles.

Posted by Community Admin on 18-Jan-2011 00:00

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

Posted by Community Admin on 18-Jan-2011 00:00

Do you just use an empty parenthesis with the role manager.

RoleManager.GetManager();

Do you do this to get all the custom roles?

Posted by Community Admin on 18-Jan-2011 00:00

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

Posted by Community Admin on 18-Jan-2011 00:00

Got it to work. Forgot to call save changes method on role manager.

// forgot to call savechanges
roleManager.SaveChanges();

This thread is closed