ASp.Net Identity Role manager

4k views Asked by At

I am using Visual Studio 2013. I have a ASP.net (vb) Webforms site with asp.net identity. I trying to create a page that manages User role and create roles. I cant find any help online for this when it comes to web forms. This code works for asp.net membership but not for Identity. Here is my code. Please help Thanks.

Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports Microsoft.AspNet.Identity.Owin
Imports Microsoft.Owin.Security
Imports Owin

Partial Class AssignRoles
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Roles.AddUserToRole(DropDownList5.SelectedValue, DropDownList6.SelectedValue)
        Label1.Text = DropDownList5.SelectedValue + " Was added to the " + DropDownList6.SelectedValue + " Role."
    End Sub

    Protected Sub btnremoverole_Click(sender As Object, e As EventArgs) Handles btnremoverole.Click
        Roles.RemoveUserFromRole(DropDownList3.SelectedValue, DropDownList4.SelectedValue)
        Label1.Text = DropDownList3.SelectedValue + " Was removed from the " + DropDownList4.SelectedValue + " Role."
    End Sub

    Protected Sub CreateRole_Click(sender As Object, e As EventArgs) Handles CreateRole.Click
        Dim createRole As String = RoleTextBox.Text

        Try
            If Roles.RoleExists(createRole) Then
                Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' already exists. Please specify a different role name."
                Return
            End If

            Roles.CreateRole(createRole)

            Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' created."

            ' Re-bind roles to GridView.

        Catch
            Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' <u>not</u> created."
        End Try
    End Sub
End Class

Here is the Markup code

<%@ Page Title="Admin Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true"CodeBehind="AssignRoles.aspx.vb" Inherits="Conflict_Minerals.AssignRoles" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">



    <div class="row">
        <div class="col-md-12">
            <h2>Admin Panel</h2>
            <br />
        </div>
        <div class="col-md-8">
            <asp:Label ID="Label3" runat="server" Text="Add user to role:  "></asp:Label>
            <asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="UserName">
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="Name">
            </asp:DropDownList>
            <asp:Button ID="Button1" runat="server" Text="Add user to role" />
        </div>
        <div class="col-md-8">
            <br />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <br />
            <asp:Label ID="Label2" runat="server" Text="Remove user from role:   "></asp:Label>

            &nbsp;&nbsp;&nbsp;<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="UserName">
            </asp:DropDownList>
             &nbsp<asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource2" DataTextField="RoleName" DataValueField="RoleName">
            </asp:DropDownList> &nbsp

            <asp:Button ID="btnremoverole" runat="server" Text="Remove user from role" Height="26px" />

            <br />
            <br />
            <div>
                <h2>Manage Roles</h2>
            </div>
            <br />
            <asp:TextBox ID="RoleTextBox" runat="server"></asp:TextBox>
            <asp:Button ID="CreateRole" runat="server" Text="Create Role" />
            <br />
            <br />
            <asp:Label ID="Msg" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <br />
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [Name] FROM [vw_AspNetRoles]"></asp:SqlDataSource>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [UserName] FROM [AspNetUsers]"></asp:SqlDataSource>
                <br />
            <br />

        </div>
    </div>
</asp:Content>
2

There are 2 answers

1
ChrisPasa On BEST ANSWER

I found a solution here is my code

Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports Microsoft.AspNet.Identity.Owin
Imports Microsoft.Owin.Security
Imports Owin

Partial Class AssignRoles
    Inherits System.Web.UI.Page




    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim UserManager = New UserManager(Of IdentityUser)(New UserStore(Of IdentityUser)(New IdentityDbContext()))
        UserManager.AddToRole(DropDownList5.SelectedValue, DropDownList6.SelectedValue)
        Label1.Text = DropDownList5.SelectedItem.Text + " Was added to the " + DropDownList6.SelectedValue + " Role."
    End Sub

    Protected Sub btnremoverole_Click(sender As Object, e As EventArgs) Handles btnremoverole.Click
        Dim UserManager = New UserManager(Of IdentityUser)(New UserStore(Of IdentityUser)(New IdentityDbContext()))
        UserManager.RemoveFromRoles(DropDownList3.SelectedValue, DropDownList4.SelectedValue)
        Label1.Text = DropDownList3.SelectedItem.Text + " Was removed from the " + DropDownList4.SelectedValue + " Role."
    End Sub

    Protected Sub CreateRole_Click(sender As Object, e As EventArgs) Handles CreateRole.Click


        Dim createRole As String = RoleTextBox.Text
        Dim RoleManager = New RoleManager(Of IdentityRole)(New RoleStore(Of IdentityRole)(New ApplicationDbContext()))
        Try
            If RoleManager.RoleExists(createRole) Then
                Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' already exists. Please specify a different role name."
                Return
            End If

            RoleManager.Create(New IdentityRole(createRole))

            Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' created."

            ' Re-bind roles to GridView.

        Catch
            Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' <u>not</u> created."
        End Try
    End Sub
End Class
3
Steve Greene On

You're going to need to instantiate a RoleManager to work with the roles. From there you can replace your AddRole and RemoveRole code. You will need a UserManager to assign roles to users. Here is a walkthrough for WebForms.http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/membership-and-administration