Add user to role programmatically

Last post 05-09-2008 3:37 PM by McGuire. 2 replies.

Sort Posts:

  • Add user to role programmatically

    05-09-2008, 1:03 PM
    • Member
      65 point Member
    • McGuire
    • Member since 01-25-2007, 4:47 PM
    • Posts 153

    I need to add the user to a role when the user creates an account.  Since we use this site to allow users to apply 27/7/365, it would not be possible to user the configuration tool.  There must be a way to add it when the user hits the submit button.  I am using asp.net's login tool createuserwizard.  Have been searching the net but so far haven't found anything I can use.  Does anyone know how to do this or is there a link I can go to?

    Your help is very much appreciated!!!!

    All that is gold does not glitter,
    Not all those who wander are lost;
    The old that is strong does not wither,
    Deep roots are not reached by the frost.
    J.R.R. Tolkien
  • Re: Add user to role programmatically

    05-09-2008, 2:57 PM
    Answer
    • All-Star
      60,792 point All-Star
    • anas
    • Member since 09-21-2006, 4:31 AM
    • Palestinian Territory, Occupied
    • Posts 6,848
    • Moderator

     you can assign the created user to a role in CreatedUserWizard CreatedUser event handler  as follows:

     

        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            string userName = CreateUserWizard1.UserName;
            Roles.AddUserToRole(userName, "Accountants");
        }

      

    Hope it Helps 

    Regards,

    Anas Ghanem | Blog

  • Re: Add user to role programmatically

    05-09-2008, 3:37 PM
    • Member
      65 point Member
    • McGuire
    • Member since 01-25-2007, 4:47 PM
    • Posts 153

    Thank you so much.  I knew it had to be something simple, but I could not get it figured out...works great.   Since I work in VB I thought I would add the code.  Not much of a change from yours. 

     

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser

       Dim userName = CreateUserWizard1.UserName

        Roles.AddUserToRole(userName, "Members")

    End Sub

    Thanks for the help! 

     

    All that is gold does not glitter,
    Not all those who wander are lost;
    The old that is strong does not wither,
    Deep roots are not reached by the frost.
    J.R.R. Tolkien
Page 1 of 1 (3 items)