Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Tuesday, September 5

ASP.NET Membership and Roles

In previous posts (Forms Authentication in ASP.NET 2.0 - Personalization and Web Parts in ASP.NET 2.0) I talked already about the ASP.NET Membership functionality, but recently I had an issue with adding a role automatically to a newly created user with the CreateUserWizard control. At that time I had a workaround for this problem, but it didn't feel really good/natural to implement it that way. So, here's the one and only solution :

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)

    {

        //Add each created user automatically to the Users role

        Roles.AddUserToRole(CreateUserWizard1.UserName, "Users");

    }


The CreateUserWizard control has an event CreatedUser that fires immediately after the new user has been created. In that event you can call the AddUserToRole method on the Web.Security.Roles object.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home