e enjte, 4 tetor 2007

CreateUser Wizard, adding roles to users created

With .NET 2.0 its all easy :)

Drag and drop a CreateUserWizard in your page and set the OnCreatedUser=CreateUserWizard1_CreatedUser

Add a Wizard Step as below

asp:WizardStep ID="AddRolesStep" Runat="server" AllowReturn="False" Title="Step 2: Assign User To Roles"
OnActivate="AssignUserToRoles_Activate" OnDeactivate="AssignUserToRoles_Deactivate"


with a Listbox or any control to display the roles

After that in code behind add

public void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
// Create an empty Profile for the newly created user
ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);
// Save the profile - must be done since we explicitly created this profile instance
p.Save();
}
// Activate event fires when the user hits "next" in the CreateUserWizard
public void AssignUserToRoles_Activate(object sender, EventArgs e)
{

// Databind list of roles in the role manager system to a listbox in the wizard
AvailableRoles.DataSource = Roles.GetAllRoles(); ;
AvailableRoles.DataBind();
}

// Deactivate event fires when user hits "next" in the CreateUserWizard
public void AssignUserToRoles_Deactivate(object sender, EventArgs e)
{

// Add user to all selected roles from the roles listbox
for (int i = 0; i < AvailableRoles.Items.Count; i++)
{
if (AvailableRoles.Items[i].Selected == true)
Roles.AddUserToRole(CreateUserWizard1.UserName, AvailableRoles.Items[i].Value);
}
}

Also dont forget to add in web.config file

< profile enabled="true" >
<properties>

<add name="Email" />
</properties>
</profile>

else you will wonder which namespace to add for ProfileCommon, compilation error will show, you are missing reference.

That's it, a Create User with associating to roles are ready!!!

1 koment:

Anonymous tha...

i dont see required information in this, r u planning to blog agai on this topic.........pls reply to my mail