asp.net identity 2.2.0 在WebForm下的角色启用和基本使用(三)
角色管理功能:
界面部分:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="jueseadmin.aspx.cs" Inherits="admin_jueseadmin" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server"> <div> <h1>角色管理</h1> <div> <h2>角色创建 </h2> <asp:Panel ID="Panel2" runat="server"> <div> <p> 角色创建 </p> <div> 角色名称:<asp:TextBox ID="TextBoxRoleName" runat="server"></asp:TextBox> <asp:Button ID="ButtonRolechuangjian" runat="server" Text="创建角色" OnClick="ButtonRolechuangjian_Click" ValidationGroup="cjjs" /> <br /> <asp:Label ID="Labelcjts" runat="server"></asp:Label> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBoxRoleName" CssClass="text-warning" ErrorMessage="RequiredFieldValidator" ValidationGroup="cjjs">您未输入需创建的角色名称。</asp:RequiredFieldValidator> </div> </div> </asp:Panel> <hr /> <asp:Panel ID="Panel1" runat="server"> <div> <p> 角色分配 </p> <div> 用户名称:<asp:TextBox ID="yonghumingcheng" runat="server"></asp:TextBox> 角色名称:<asp:TextBox ID="juesemingcheng" runat="server"></asp:TextBox> <asp:Button ID="Buttonjuesefenpei" runat="server" Text="划入角色" OnClick="Buttonjuesefenpei_Click" ValidationGroup="hrjs" /> <br /> <asp:Label ID="Labelhrts" runat="server"></asp:Label> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" CssClass="text-warning" ControlToValidate="yonghumingcheng" ValidationGroup="hrjs">您未输入用户名称。</asp:RequiredFieldValidator> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator" CssClass="text-warning" ControlToValidate="juesemingcheng" ValidationGroup="hrjs">您未输入角色名称。</asp:RequiredFieldValidator> </div> </div> </asp:Panel> <hr /> <asp:Panel ID="Panel3" runat="server"> <div> <p> 删除角色 </p> <div> 角色名称:<asp:TextBox ID="TextBoxjueseshanchu" runat="server"></asp:TextBox> <asp:Button ID="Buttonjueseshanchu" runat="server" Text="删除角色" OnClick="Buttonjueseshanchu_Click" ValidationGroup="scjs" /> <br /> <asp:Label ID="Labelscts" runat="server"></asp:Label> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBoxjueseshanchu" CssClass="text-warning" ErrorMessage="RequiredFieldValidator" ValidationGroup="scjs">您未输入需创建的角色名称。</asp:RequiredFieldValidator> </div> </div> </asp:Panel> </div> </div> </asp:Content>
cs代码部分:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using xxxxform; //你的项目 public partial class admin_jueseadmin : System.Web.UI.Page { ApplicationDbContext context = new ApplicationDbContext(); IdentityResult IdRoleResult; IdentityResult IdUserResult; protected void Page_Load(object sender, EventArgs e) { } protected void ButtonRolechuangjian_Click(object sender, EventArgs e) { var roleStore = new RoleStore<IdentityRole>(context); var roleManager = new RoleManager<IdentityRole>(roleStore); if (!roleManager.RoleExists(TextBoxRoleName.Text)) { var IdRoleResult = roleManager.Create(new IdentityRole { Name = TextBoxRoleName.Text }); Labelcjts.Text = "角色已经创建完成"; } else { Labelcjts.Text = "该角色已存在,无需创建。"; } } protected void Buttonjuesefenpei_Click(object sender, EventArgs e) { var roleStore = new RoleStore<IdentityRole>(context); var roleManager = new RoleManager<IdentityRole>(roleStore); var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); if (!userManager.IsInRole(userManager.FindByName(yonghumingcheng.Text).Id, juesemingcheng.Text)) { IdUserResult = userManager.AddToRole(userManager.FindByName(yonghumingcheng.Text).Id, juesemingcheng.Text); Labelhrts.Text = "用户划入角色完成"; } } protected void Buttonjueseshanchu_Click(object sender, EventArgs e) { var roleStore = new RoleStore<IdentityRole>(context); var roleManager = new RoleManager<IdentityRole>(roleStore); IdRoleResult = roleManager.Delete(new IdentityRole { Name = TextBoxjueseshanchu.Text }); Labelcjts.Text = "角色已经删除完成"; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。