none
Creacion de nuevos perfiles de usuario

    Question

  • Hola estoy programando en asp.net con vb.net... tengo este codigo... con el control de nuevos usuarios....

     

     Roles.AddUserToRole(CreateUserWizard1.UserName, "Inspectores");

     

     

    mi pregunta como haria esta misma funcion en VB.NET y ademas agregar mas perfiles de usuarios... saludos


    Ignacio Garrido Mi Blog: http://so-iso.blogspot.com/

    Tuesday, December 07, 2010 5:02 PM

Answers

  • Bueno, así tiene que ser, unicamente el administrador debería tenerlo.

    Si estas usando el CreateUserWizard de asp.net, entonces nada mas debes hacer unos pequeños cambios. Por ejemplo

    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" ContinueDestinationPageUrl="~/Admin/PrincAdmin.aspx"
                        DisableCreatedUser="false" Font-Names="Verdana" BackColor="white" Font-Size="10pt"
                        BorderWidth="1px" BorderColor="#CCCC99" BorderStyle="Solid" CompleteSuccessText="The account has been successfully created."
                        UnknownErrorMessage="The account was not created. Please try again.">
                        <WizardSteps>
                            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" Title="Step 1: Basic User Information">
                                <ContentTemplate>
                                    <table style="font-size: 10pt; font-family: Verdana; text-align:center;" border="0" width="400" >
                                        <tr>
                                            <td style="font-weight: bold; color: white; background-color: #6b696b" align="center"
                                                colspan="2">
                                                Create Your UserID
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">UserID:</asp:Label>
                                            </td>
                                            <td>
                                                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ToolTip="User Name is required."
                                                    ErrorMessage="User Name is required." ValidationGroup="CreateUserWizard1" ControlToValidate="UserName">
                                            *</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" ToolTip="Password is required."
                                                    ErrorMessage="Password is required." ValidationGroup="CreateUserWizard1" ControlToValidate="Password">
                                            *</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Re-Type Password:</asp:Label>
                                            </td>
                                            <td>
                                                <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ToolTip="Confirm Password is required."
                                                    ErrorMessage="Confirm Password is required." ValidationGroup="CreateUserWizard1"
                                                    ControlToValidate="ConfirmPassword">
                                            *</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">Email:</asp:Label>
                                            </td>
                                            <td>
                                                <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ToolTip="Email is required."
                                                    ErrorMessage="Email is required." ValidationGroup="CreateUserWizard1" ControlToValidate="Email">
                                            *</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                    </table>
                                    <br />
                                    <table style="font-size: 10pt; font-family: Verdana" border="0" width="400">
                                        <tr>
                                            <td style="font-weight: bold; color: white; background-color: #6b696b" align="center"
                                                colspan="2">
                                                If You Forget Your Password
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                Security Question:
                                            </td>
                                            <td>
                                                <asp:DropDownList ID="Question" runat="server" Width="200">
                                                    <asp:ListItem Text="[Select a Question]" />
                                                    <asp:ListItem Text="Favorite Pet" />
                                                    <asp:ListItem Text="Mother's Maiden Name" />
                                                    <asp:ListItem Text="Who was your childhood hero?" />
                                                    <asp:ListItem Text="Your favorite pasttime?" />
                                                </asp:DropDownList>
                                                <asp:RequiredFieldValidator ID="QuestionRequired" runat="server" InitialValue="[Select a Question]"
                                                    ControlToValidate="Question" ErrorMessage="Select a Security Question" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right" width="170">
                                                Your Answer:
                                            </td>
                                            <td>
                                                <asp:TextBox ID="Answer" runat="server"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer"
                                                    ErrorMessage="RequiredFieldValidator" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                    </table>
                                </ContentTemplate>
                            </asp:CreateUserWizardStep>
                            <asp:WizardStep runat="server" ID="wsAssignUserToRoles" AllowReturn="False" Title="Step 2: Assign User To Roles"
                        OnActivate="AssignUserToRoles_Activate" OnDeactivate="AssignUserToRoles_Deactivate">
                        <table>
                            <tr>
                                <td>
                                    Select one or more roles for the user:</td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:ListBox ID="AvailableRoles" runat="server" SelectionMode="Multiple" Height="104px" Width="264px"></asp:ListBox>
                                </td>
                            </tr>
                        </table>
                    </asp:WizardStep>
                   
                            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                            </asp:CompleteWizardStep>
                        </WizardSteps>
                        <TitleTextStyle Font-Bold="True" BackColor="#6B696B" ForeColor="White"></TitleTextStyle>
                    </asp:CreateUserWizard>

     

     

    y en el code behind

    ' Activate event fires when the user hits "next" in the CreateUserWizard
        Public Sub AssignUserToRoles_Activate(ByVal sender As Object, ByVal e As EventArgs)

            ' Databind list of roles in the role manager system to a listbox in the wizard
            AvailableRoles.DataSource = Roles.GetAllRoles()
            AvailableRoles.DataBind()
        End Sub

        ' Deactivate event fires when user hits "next" in the CreateUserWizard
        Public Sub AssignUserToRoles_Deactivate(ByVal sender As Object, ByVal e As EventArgs)

            ' Add user to all selected roles from the roles listbox
            For i As Integer = 0 To AvailableRoles.Items.Count - 1
                If AvailableRoles.Items(i).Selected = True Then
                    Roles.AddUserToRole(CreateUserWizard1.UserName, AvailableRoles.Items(i).Value)
                End If
            Next
        End Sub

     

    saludos,

     

     



    Jason Ulloa
    Mi Blog
    • Marked as answer by detalle Tuesday, December 07, 2010 8:09 PM
    Tuesday, December 07, 2010 7:22 PM

All replies

  • Hola,

     

    La diferencia de la línea que colocaste en c# y vb es mínima, practicamente sería el ;

    Roles.AddUserToRole(CreateUserWizard1.UserName, "Inspectores")

     

    Ahora, cuando dices como haría para agregar mas perfiles, te refieres a mas roles?

    Si es asi puedes usar

     Try
                If Roles.RoleExists(NewRole.Text.ToString()) = False Then
                    Roles.CreateRole(NewRole.Text.ToString())
                    ConfirmationMessage.InnerText = "The new role was added."
                    createRoleSuccess = True
                End If
            Catch ex As Exception
                ConfirmationMessage.InnerText = ex.Message
                createRoleSuccess = False
            End Try

     

    saludos,



    Jason Ulloa
    Mi Blog
    Tuesday, December 07, 2010 5:30 PM
  • Gracias Jason sabes, el primer ejemplo me ayudo pero por ejemplo cuando crea un nuevo usuario tengo 3 perfiles o mas

    por ejemplo :

    Administrador

    Coordinadores

    Inspectores

    Obras

     

    como haria para que tenga esta opcion de roles que son perfiles de usuario me entendiste mi consulta? mucha gracias


    Ignacio Garrido Mi Blog: http://so-iso.blogspot.com/
    Tuesday, December 07, 2010 5:38 PM
  • Como mi cabeza anda fuera :) no he logrado captar tu nueva pregunta! Pero me parecio que lo que quieres es:

     

    Que al momento de estar creando un nuevo usuario puedas tener la posibilidad de seleccionar los roles que tendrá?? Dime si es así

     

    Saludos,



    Jason Ulloa
    Mi Blog
    Tuesday, December 07, 2010 5:46 PM
  • Como mi cabeza anda fuera :) no he logrado captar tu nueva pregunta! Pero me parecio que lo que quieres es:

     

    Que al momento de estar creando un nuevo usuario puedas tener la posibilidad de seleccionar los roles que tendrá?? Dime si es así

     

    Saludos,



    Jason Ulloa
    Mi Blog


    si asi es jason, como dices que tenga la posibilidad de selecionar los roles que quiera, pero en este caso seria que haga eso el administrador

     

     


    Ignacio Garrido Mi Blog: http://so-iso.blogspot.com/
    Tuesday, December 07, 2010 6:24 PM
  • Bueno, así tiene que ser, unicamente el administrador debería tenerlo.

    Si estas usando el CreateUserWizard de asp.net, entonces nada mas debes hacer unos pequeños cambios. Por ejemplo

    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" ContinueDestinationPageUrl="~/Admin/PrincAdmin.aspx"
                        DisableCreatedUser="false" Font-Names="Verdana" BackColor="white" Font-Size="10pt"
                        BorderWidth="1px" BorderColor="#CCCC99" BorderStyle="Solid" CompleteSuccessText="The account has been successfully created."
                        UnknownErrorMessage="The account was not created. Please try again.">
                        <WizardSteps>
                            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" Title="Step 1: Basic User Information">
                                <ContentTemplate>
                                    <table style="font-size: 10pt; font-family: Verdana; text-align:center;" border="0" width="400" >
                                        <tr>
                                            <td style="font-weight: bold; color: white; background-color: #6b696b" align="center"
                                                colspan="2">
                                                Create Your UserID
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">UserID:</asp:Label>
                                            </td>
                                            <td>
                                                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ToolTip="User Name is required."
                                                    ErrorMessage="User Name is required." ValidationGroup="CreateUserWizard1" ControlToValidate="UserName">
                                            *</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" ToolTip="Password is required."
                                                    ErrorMessage="Password is required." ValidationGroup="CreateUserWizard1" ControlToValidate="Password">
                                            *</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Re-Type Password:</asp:Label>
                                            </td>
                                            <td>
                                                <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ToolTip="Confirm Password is required."
                                                    ErrorMessage="Confirm Password is required." ValidationGroup="CreateUserWizard1"
                                                    ControlToValidate="ConfirmPassword">
                                            *</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">Email:</asp:Label>
                                            </td>
                                            <td>
                                                <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ToolTip="Email is required."
                                                    ErrorMessage="Email is required." ValidationGroup="CreateUserWizard1" ControlToValidate="Email">
                                            *</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                    </table>
                                    <br />
                                    <table style="font-size: 10pt; font-family: Verdana" border="0" width="400">
                                        <tr>
                                            <td style="font-weight: bold; color: white; background-color: #6b696b" align="center"
                                                colspan="2">
                                                If You Forget Your Password
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                Security Question:
                                            </td>
                                            <td>
                                                <asp:DropDownList ID="Question" runat="server" Width="200">
                                                    <asp:ListItem Text="[Select a Question]" />
                                                    <asp:ListItem Text="Favorite Pet" />
                                                    <asp:ListItem Text="Mother's Maiden Name" />
                                                    <asp:ListItem Text="Who was your childhood hero?" />
                                                    <asp:ListItem Text="Your favorite pasttime?" />
                                                </asp:DropDownList>
                                                <asp:RequiredFieldValidator ID="QuestionRequired" runat="server" InitialValue="[Select a Question]"
                                                    ControlToValidate="Question" ErrorMessage="Select a Security Question" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right" width="170">
                                                Your Answer:
                                            </td>
                                            <td>
                                                <asp:TextBox ID="Answer" runat="server"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer"
                                                    ErrorMessage="RequiredFieldValidator" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                    </table>
                                </ContentTemplate>
                            </asp:CreateUserWizardStep>
                            <asp:WizardStep runat="server" ID="wsAssignUserToRoles" AllowReturn="False" Title="Step 2: Assign User To Roles"
                        OnActivate="AssignUserToRoles_Activate" OnDeactivate="AssignUserToRoles_Deactivate">
                        <table>
                            <tr>
                                <td>
                                    Select one or more roles for the user:</td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:ListBox ID="AvailableRoles" runat="server" SelectionMode="Multiple" Height="104px" Width="264px"></asp:ListBox>
                                </td>
                            </tr>
                        </table>
                    </asp:WizardStep>
                   
                            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                            </asp:CompleteWizardStep>
                        </WizardSteps>
                        <TitleTextStyle Font-Bold="True" BackColor="#6B696B" ForeColor="White"></TitleTextStyle>
                    </asp:CreateUserWizard>

     

     

    y en el code behind

    ' Activate event fires when the user hits "next" in the CreateUserWizard
        Public Sub AssignUserToRoles_Activate(ByVal sender As Object, ByVal e As EventArgs)

            ' Databind list of roles in the role manager system to a listbox in the wizard
            AvailableRoles.DataSource = Roles.GetAllRoles()
            AvailableRoles.DataBind()
        End Sub

        ' Deactivate event fires when user hits "next" in the CreateUserWizard
        Public Sub AssignUserToRoles_Deactivate(ByVal sender As Object, ByVal e As EventArgs)

            ' Add user to all selected roles from the roles listbox
            For i As Integer = 0 To AvailableRoles.Items.Count - 1
                If AvailableRoles.Items(i).Selected = True Then
                    Roles.AddUserToRole(CreateUserWizard1.UserName, AvailableRoles.Items(i).Value)
                End If
            Next
        End Sub

     

    saludos,

     

     



    Jason Ulloa
    Mi Blog
    • Marked as answer by detalle Tuesday, December 07, 2010 8:09 PM
    Tuesday, December 07, 2010 7:22 PM