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
    • Loading...
    • McGuire
    • Joined on 01-25-2007, 9:47 PM
    • Posts 48

    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!!!!

  • Re: Add user to role programmatically

    05-09-2008, 2:57 PM
    Answer
    • Loading...
    • anas
    • Joined on 09-21-2006, 4:31 AM
    • Palestine - فلسطين
    • Posts 2,438

     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 

    Best Regards,

    Anas Ghanem - انس الغانم | My Blog
  • Re: Add user to role programmatically

    05-09-2008, 3:37 PM
    • Loading...
    • McGuire
    • Joined on 01-25-2007, 9:47 PM
    • Posts 48

    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! 

     

Page 1 of 1 (3 items)