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 = "角色已经删除完成";
    }
}
时间: 2024-08-02 12:39:53

asp.net identity 2.2.0 在WebForm下的角色启用和基本使用(三)的相关文章

asp.net identity 2.2.0 在WebForm下的角色启用和基本使用(一)

基本环境:asp.net 4.5.2 第一步:在App_Start文件夹中的IdentityConfig.cs中添加角色控制器. 在namespace xxx内(即最后一个“}”前面)添加 角色控制类 代码如下: //配置此应用程序中使用的应用程序角色管理器.RoleManager 在 ASP.NET Identity 中定义,并由此应用程序使用. public class ApplicationRoleManager : RoleManager<IdentityRole> { public

asp.net identity 2.2.0 在WebForm下的角色启用和基本使用(二)

管理用户功能: 界面部分: <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_Default" %> <asp:Content ID="Content1&

asp.net identity 2.2.0 在WebForm下的角色启用和基本使用(四)

有网友问及权限的问题,其实我觉得没什么改进. 主目录下的web.config基本不用改.要说要改的也就只有数据库连接了. <authentication mode="None" />    <compilation debug="true" targetFramework="4.5.2" /> 这两项我都没改,按说<authentication mode="None" />应该改为<a

Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理

这是本人第一次写,写的不好的地方还忘包含.写这个的主要原因是翔通过这个来学习下EF的CodeFirst模式,本来也想用AngularJs来玩玩的,但是自己只会普通的绑定,对指令这些不是很熟悉,所以就基本不用了.还有最主要的原因就是锻炼下自己的能力.好了其他就不多说了,下面来看下我对这个项目的整体概述吧: 目录: 目录我以后会在这边添加上去的 一.Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理 基本设计 项目中使用到的工具: Visual Studio 2013,

asp.net identity 2.2.0 中角色启用和基本使用(六)

创建用户管理相关视图 第一步:添加视图   打开UsersAdminController.cs   将鼠标移动到public ActionResult Index()上  右键>添加视图   系统会弹出对话框  什么也不用改 直接“添加” 第二步:在创建的视图上定义一个公开枚举模型 在第一行添加 @model IEnumerable<xxxx(项目名).Models .ApplicationUser> 第三步:建立页面视图模板,代码完成后如下. @model IEnumerable<

asp.net identity 2.2.0 中角色启用和基本使用(一)

基本环境:asp.net 4.5.2 第一步:在App_Start文件夹中的IdentityConfig.cs中添加角色控制器. 在namespace xxx内(即最后一个“}”前面)添加 角色控制类 代码如下: //配置此应用程序中使用的应用程序角色管理器.RoleManager 在 ASP.NET Identity 中定义,并由此应用程序使用. public class ApplicationRoleManager : RoleManager<IdentityRole> { public

asp.net identity 2.2.0 中角色启用和基本使用(四)

创建角色相关视图 第一步:添加视图   打开RolesAdminController.cs   将鼠标移动到public ActionResult Index()上  右键>添加视图   系统会弹出对话框  什么也不用改 直接“确定” 第二步:在创建的视图上定义一个公开枚举模型 在第一行添加 @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole> 第三步:建立页面视图模板,代码完成后如下. @m

asp.net identity 2.2.0 中角色启用和基本使用(三)

创建控制器 第一步:在controllers文件夹上点右键>添加>控制器, 我这里选的是“MVC5 控制器-空”,名称设置为:RolesAdminController.cs 第二步:添加命名空间 using System.Net; using System.Threading.Tasks; using xxxx.Models;//你项目的名称 using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using

asp.net identity 2.2.0 中角色启用和基本使用(二)

建立模型 第一步:在Models文件夹上点右键 >添加>类     类的名称自定,我用AdminViewModels命名的 因为是讲基本使用,我这里不做任何扩展. 第二步:添加如下命名空间 using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; 删除模板建立类时自带的但用不到的命名空间(可选) using System; using System.L