ASp.Net Identity角色管理器

时间:2015-06-10 19:22:49

标签: asp.net vb.net webforms sql-server-2012 asp.net-identity

我正在使用Visual Studio 2013.我有一个带有asp.net身份的ASP.net(vb)Webforms站点。我尝试创建一个管理用户角色和创建角色的页面。在网络表单方面,我无法在网上找到任何帮助。此代码适用于asp.net成员资格,但不适用于Identity。这是我的代码。 请帮助谢谢。

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

这是标记代码

<%@ 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 个答案:

答案 0 :(得分:1)

我发现这里的解决方案是我的代码

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

答案 1 :(得分:0)

您将需要实例化RoleManager以使用角色。从那里你可以替换你的AddRole和RemoveRole代码。您需要一个UserManager来为用户分配角色。以下是WebForms的演练。http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/membership-and-administration