Add user to role on signup

M

^MisterJingo^

Hi all,

I've been searching the net for a way to programatically add a user to
a role on successful registration. Not finding anything useful I
decided to experiment and came up with the following. Using the
SQLprovider for membership/roles and the createUserWizard:

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs
e)
{
Roles.AddUserToRole(CreateUserWizard1.UserName, "User");
}

This seems to work fine, but wanted to check if it was ok to do it
this way.

Thanks.
 
P

PeterKellner

Hi all,

I've been searching the net for a way to programatically add a user to
a role on successful registration. Not finding anything useful I
decided to experiment and came up with the following. Using the
SQLprovider for membership/roles and the createUserWizard:

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs
e)
{
Roles.AddUserToRole(CreateUserWizard1.UserName, "User");
}

This seems to work fine, but wanted to check if it was ok to do it
this way.

Thanks.

If you look at the asp.net sample apps, they create the users in the
global.asax file. That is the pattern I follow. Here is the code
from a sample app:

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>



<script runat="server">

void Application_Start(Object sender, EventArgs e) {
// Code that runs on application startup
if (Roles.Enabled)
{
if (!Roles.RoleExists("ProjectAdministrator"))
{
Roles.CreateRole("ProjectAdministrator");
}
if (!Roles.RoleExists("ProjectManager"))
{
Roles.CreateRole("ProjectManager");
}

if (!Roles.RoleExists("Consultant"))
{
Roles.CreateRole("Consultant");
}
}


}

void Application_End(Object sender, EventArgs e) {
// Code that runs on application shutdown

}

Peter Kellner
http://peterkellner.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top