To assign a role to a new user, we usually use the following code. We call this on the CreatedUser event of the ASP.NET CreateUserWizard Control:

protected void CreatedUser(object sender, EventArgs e) {
Roles.AddUserToRole(cuwRegister.UserName, “Registered User”);
}

The code worked fine for me but not when I customized the CreateUserWizard Control where I encountered this error:

The parameter ‘username’ must not be empty.
Parameter name: username

For some reason, the cuwRegister.UserName gives an empty string thus the error. So I used the FindControl method to access the UserName textbox:

TextBox userName = (TextBox)cuwRegister.CreateUserStep.ContentTemplateContainer.FindControl(”UserName”);
Roles.AddUserToRole(userName.Text, “Registered User”);

To learn how to customize the CreateUserWizard Control, go to MSDN.



Tags: