Add user to role on signup

Discussion in 'ASP .Net' started by ^MisterJingo^, Jun 4, 2006.

  1. 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.
    ^MisterJingo^, Jun 4, 2006
    #1
    1. Advertising

  2. ^MisterJingo^

    PeterKellner Guest

    On 4 Jun 2006 10:21:07 -0700, "^MisterJingo^" <>
    wrote:

    >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
    PeterKellner, Jun 5, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesper Stocholm
    Replies:
    2
    Views:
    8,064
    John Saunders
    Aug 23, 2003
  2. Whitecrest
    Replies:
    4
    Views:
    489
    GreyWyvern
    Dec 11, 2003
  3. GSU Brian
    Replies:
    0
    Views:
    1,691
    GSU Brian
    May 1, 2008
  4. .Net Sports

    Membership signup script thru email

    .Net Sports, Oct 3, 2006, in forum: ASP General
    Replies:
    2
    Views:
    135
    http://support.cjwsoft.com/code/code_info.asp?TID=
    Oct 4, 2006
  5. Joe
    Replies:
    2
    Views:
    99
    Ron Savage
    Aug 29, 2005
Loading...

Share This Page