Create user programmatically
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);
Hi Doug,
You have to use RoleManager methods like RoleExists
if (roleManager.RoleExists(role.Name))
Greetings,
Ivan Dimitrov
the Telerik team
Thanks
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);
I found manage roles and manage users in permissions and set to 'Everyone'. I think this solves it.
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?
Anyone got any ideas?
I've found 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
;
Role role = roleManager.GetRole(memberRole);
if
(!roleManager.IsUserInRole(user.Id, memberRole))
roleManager.AddUserToRole(user, role);
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);
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();
What about custom roles not the ones provided?
I've created several custom roles and can't seem to add users to those roles.
Hi Doug,
The custom roles you created are part of the Default provider.
Greetings,
Ivan Dimitrov
the Telerik team
Do you just use an empty parenthesis with the role manager.
RoleManager.GetManager();
Hello Doug,
GetManager()
gets a manger instance for the default data provider which should contain your custom roles.
Regards,
Ivan Dimitrov
the Telerik team
Got it to work. Forgot to call save changes method on role manager.
// forgot to call savechanges
roleManager.SaveChanges();