Automatic Role Assignment?

Last post 07-19-2006 1:22 AM by Buck516. 9 replies.

Sort Posts:

  • Automatic Role Assignment?

    05-01-2006, 10:51 AM
    • Member
      50 point Member
    • kingbal
    • Member since 05-01-2006, 2:47 PM
    • Posts 10

    Hi, im currently making a site that has two user authentication roles, members and admin.

    Ive added a create user wizard to one of the pages, but when i sign a new user up, they are not assigned a role and hence can access both admin and members areas.

    How would i go about setting it up so that all new users that are created through the wizard are automatically assigned the members role? thanks in advance! Jo

  • Re: Automatic Role Assignment?

    05-01-2006, 12:47 PM
    • All-Star
      23,809 point All-Star
    • pkellner
    • Member since 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,576
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    you could add code to the createduser event like this of the wizard:

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)

    {

    string[] usersArray = new string[1];

    usersArray[0] = CreateUserWizard1.UserName;

    Roles.AddUsersToRole(usersArray, "Admin");

    }

    }

    Peter Kellner
    http://peterkellner.net
    Microsoft MVP • ASPInsider
  • Re: Automatic Role Assignment?

    05-01-2006, 1:47 PM
    • Member
      65 point Member
    • etheren
    • Member since 02-18-2004, 5:42 PM
    • Posts 13

    Basically what I do is use the UserCreated event for the CreateUserWizard.  And it would look something like

     

    protected void CreateUserWizard_CreatedUser(object sender, EventArgs e)
    {
        CreateUserWizard cuw = sender as CreateUserWizard;
        if (cuw != null)
        {
            Roles.AddUserToRoles(cuw.UserName, getDefaultRoles());
        }
    }
    
    And in your case getDefaultRoles would look like...
    private string[] getDefaultRoles()
    {
        return new string[] { "members" };
    }

     

    If you just have 1 role and don't believe it'll ever change just use Roles.AddUserToRole(cuw.UserName, "members");

  • Re: Automatic Role Assignment?

    05-01-2006, 3:17 PM
    • Member
      50 point Member
    • kingbal
    • Member since 05-01-2006, 2:47 PM
    • Posts 10
    Cheers etheren worked like a dream!
  • Re: Automatic Role Assignment?

    05-01-2006, 3:24 PM
    • All-Star
      23,809 point All-Star
    • pkellner
    • Member since 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,576
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    I like Etheren's better than mine.  Nice.
    Peter Kellner
    http://peterkellner.net
    Microsoft MVP • ASPInsider
  • Re: Automatic Role Assignment?

    07-01-2006, 4:38 PM
    • Member
      30 point Member
    • Buck516
    • Member since 06-26-2006, 6:47 AM
    • Posts 6

    Hi, Im new to all this and my site requires 3 types of membership... Basic, Gold and Platinum

    Right now I just want to have the Basic membership, which is free to be automatically assigned.

    I am assuming that this event works, problem is, I have no idea where to find or put this event.

    I used the create new user wizard but I cant find anything that remotely resembles this?

    Even when I look at the page in VS it says no events.

    Help and thanks alot from us novice users :)

  • Re: Automatic Role Assignment?

    07-01-2006, 4:44 PM
    • All-Star
      23,809 point All-Star
    • pkellner
    • Member since 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,576
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    The above post (which I'll paste below) shows the code.  This code needs to be added in the CreateUserWizard event.

    you could add code to the createduser event like this of the wizard (CreatedUser event)

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)

    {

    string[] usersArray = new string[1];

    usersArray[0] = CreateUserWizard1.UserName;

    Roles.AddUsersToRole(usersArray, "Admin");

    }

    }


    http://peterkellner.net
    -Peter Kellner
    Peter Kellner
    http://peterkellner.net
    Microsoft MVP • ASPInsider
  • Re: Automatic Role Assignment?

    07-03-2006, 3:25 AM
    • Member
      30 point Member
    • Buck516
    • Member since 06-26-2006, 6:47 AM
    • Posts 6

    Hi and thanks again...

    At the risk of proving my incompetence, I guess I'm confused by the term "event"...

    where exactly do I paste this code? My NewUser was created in VS so unless its and external file or application, I'm assuming this would go in the html code page? I didnt see anything in the web.config that looked like this......

  • Re: Automatic Role Assignment?

    07-17-2006, 5:25 PM
    • Member
      65 point Member
    • etheren
    • Member since 02-18-2004, 5:42 PM
    • Posts 13
    No, it's not in the web.config.  I must say, if you don't know what an event is in .NET you should definately look into. 

    Anyhow.  Here is the user code I use for assigning a default role.
    protected void CreateUserWizard_CreatedUser(object sender, EventArgs e)
    {
    CreateUserWizard cuw = sender as CreateUserWizard;
    if (cuw != null)
    {
    Roles.AddUserToRoles(cuw.UserName, getDefaultRoles());
    }
    }
    private string[] getDefaultRoles()
    {
    return ConfigurationManager.AppSettings[@"DefaultRoles"].Split(',');
    }

    Change getDefaultRoles to return a string[] containing your default roles.
    So just declare your CreateUserWizard
    <asp:CreateUserWizard id="ccw" runat="server" OnCreatedUser="CreateUserWizard_CreatedUser" />
    Notice the OnCreateUser attribute.
    Or you could use

    <asp:CreateUserWizard id="ccw" runat="server" />

    and in the code behind around the OnInit event
    ccw.CreatedUser += new EventHandler(CreateUserWizard_CreatedUser);

    Knowing how to use events is a must know for .NET development.
  • Re: Automatic Role Assignment?

    07-19-2006, 1:22 AM
    • Member
      30 point Member
    • Buck516
    • Member since 06-26-2006, 6:47 AM
    • Posts 6

    Hi again and thanks for the reply...

    Im sorry, Im not a programmer by any means, I am just a novice at this and although I understand how the even works, I cant seem to place this anywhere on my page without it getting errors.

    For the sake of better understanding, I will paste my page source code here and if anyone could show me where to place the new code to make "Members" my default role, I would greatly appreciate it.

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>New User Signup</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="background-image: url(images/1bucks.jpg); text-align: center">
            <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" CancelButtonText="CANCEL"
                CancelDestinationPageUrl="http://www.yahoo.com" ContinueButtonText="DONE" ContinueDestinationPageUrl="~/NewUserComplete.aspx"
                CreateUserButtonText="    OK    " DisplayCancelButton="True" Style="font-weight: bold;
                font-size: 10pt; z-index: 100; left: 472px; color: #3300cc; font-style: italic;
                font-family: Georgia; position: absolute; top: 151px">
                <WizardSteps>
                    <asp:CreateUserWizardStep runat="server">
                        <ContentTemplate>
                            <table border="0">
                                <tr>
                                    <td align="center" colspan="2">
                                        Sign Up for Your New Account</td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label></td>
                                    <td>
                                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                            ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                                    <td>
                                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                            ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label></td>
                                    <td>
                                        <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
                                            ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required."
                                            ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label></td>
                                    <td>
                                        <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"
                                            ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label></td>
                                    <td>
                                        <asp:TextBox ID="Question" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="Question"
                                            ErrorMessage="Security question is required." ToolTip="Security question is required."
                                            ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label></td>
                                    <td>
                                        <asp:TextBox ID="Answer" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer"
                                            ErrorMessage="Security answer is required." ToolTip="Security answer is required."
                                            ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2">
                                        <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
                                            ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
                                            ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2" style="color: red">
                                        <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                                    </td>
                                </tr>
                            </table>
                        </ContentTemplate>
                    </asp:CreateUserWizardStep>
                    <asp:CompleteWizardStep runat="server">
                    </asp:CompleteWizardStep>
                </WizardSteps>
                <CreateUserButtonStyle Font-Bold="True" />
                <CancelButtonStyle Font-Bold="True" />
            </asp:CreateUserWizard>
            <br />
            <br />
            <asp:Image ID="Image1" runat="server" Height="91px" ImageUrl="~/images/image311.gif"
                Style="z-index: 102; left: 507px; position: absolute; top: 30px" Width="272px" />
            <br />
            <br />
            <br />
            <br />
            <br />
            <br />
            <br />
            <br />
            Note: UserName MUST Be Unique*<br />
            Password MUST Be At Least 6 Characters*<br />
            E-Mail MUST Be The Same As Payment E-Mail*<br />
            (If Runtime Error Occurs, Hit Your "Back" Button and Resubmit)<br />
            <br />
            <br />
            <br />
            <br />
            <br />
            &nbsp;</div>
        </form>
    </body>
    </html>

    Thanks Again

Page 1 of 1 (10 items)