[英]ASp.Net Identity Role manager
我使用的是 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>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="UserName">
</asp:DropDownList>
 <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource2" DataTextField="RoleName" DataValueField="RoleName">
</asp:DropDownList>  
<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>
我在這里找到了一個解決方案是我的代碼
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
您將需要實例化 RoleManager 以使用角色。 從那里您可以替換您的 AddRole 和 RemoveRole 代碼。 您將需要一個 UserManager 來為用戶分配角色。 這是 WebForms 的演練。 http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/membership-and-administration
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.