using Microsoft.AspNet.Identity;

public ActionResult AddRole(String name)
{
  using (var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new IdentityDbContext())))
{
    
if (!roleManager.RoleExists(name))
    {
      
roleManager.Create(new IdentityRole(name));
    
}
  
}
} 另一种方法: 1、在 App_Start\IdentityConfig.cs 文件中配置应用程序角色管理器
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
  public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore) : base(roleStore)
  {
  }
  public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
  {
    return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
  }
} 2、在 App_Start\Startup.Auth.cs 文件中添加 app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
public void ConfigureAuth(IAppBuilder app)
{
  // 配置数据库上下文、用户管理器和登录管理器,以便为每个请求使用单个实例
  app.CreatePerOwinContext(ApplicationDbContext.Create);
  app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
  app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
  app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
  ......
} 3、在需要用的地方,输入以下代码,用于创建 RoleManager 实例
private ApplicationRoleManager _roleManager;
public ApplicationRoleManager RoleManager
{
  get
  {
    return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
  }
  private set
  {
    _roleManager = value;
  }
} 这样就可以使用以下代码创建、编辑和删除角色了。
var result = await RoleManager.CreateAsync(new IdentityRole(name));
var result = await RoleManager.UpdateAsync(role);
var result = await RoleManager.DeleteAsync(role);

ASP.NET MVC Identity 添加角色的更多相关文章

  1. ASP.NET MVC 5 - 添加一个模型

    在本节中,您将添加一些类,这些类用于管理数据库中的电影.这些类是ASP.NET MVC 应用程序中的"模型(Model)". 您将使用.NET Framework 数据访问技术En ...

  2. 返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo

    原文:返璞归真 asp.net mvc (1) - 添加.查询.更新和删除的 Demo [索引页] [源码下载] 返璞归真 asp.net mvc (1) - 添加.查询.更新和删除的 Demo 作者 ...

  3. [转]ASP.NET MVC 5 - 添加一个模型

    在本节中,您将添加一些类,这些类用于管理数据库中的电影.这些类是ASP.NET MVC 应用程序中的"模型(Model)". 您将使用.NET Framework 数据访问技术En ...

  4. ASP.NET MVC Identity 兩個多個連接字符串問題解決一例

    按照ASP.NET MVC Identity建立了一個用戶權限管理模塊,由于還要加自己已有的數據庫,所以建立了一個實體模型,建立了之后,發現登錄不了: 一直顯示“Login in failed for ...

  5. Asp.Net MVC Identity 2.2.1 使用技巧(二)

    之前我们看到了新生成的项目中跟identity有关的有四个文件,这些文件是基础功能,并未开启identity的全部功能.现在我们先启用角色功能. 1.在App_Start文件夹中的IdentityCo ...

  6. ASP.NET MVC系列:添加控制器

    基于MVC的应用程序包含三个部分 Models(模型):对应用程序的数据进行处理 Views(视图):动态生成HTML,显示数据 Controllers(控制器):应用程序中处理用户交互的部分,处理浏 ...

  7. ASP.NET MVC系列:添加视图

    虽然在上一篇文章中我们知道通过控制器可以在浏览器输出HTML页面,但是这不是控制器主要干的事,因为页面上我为还要做很多好看的特效,页面展示的事情当然交给视图来做了:下面我们就来看看如何添加一个视图 添 ...

  8. ASP.NET MVC系列:添加模型

    模型(Model)是应用程序中用于处理应用程序数据逻辑的部分;通常模型对象在数据库中存取数据 添加模型类 在解决方案中右击Models文件夹,然后选择“添加”,在“添加”项里选择“类”:或者选中Mod ...

  9. ASP.NET MVC系列:添加模型的验证规则

    首先,在模型类中引用 System.ComponentModel.DataAnnotations 命名空间;System.ComponentModel.DataAnnotations 命名空间提供定义 ...

随机推荐

  1. 【转】关于usr/bin/ld: cannot find -lxxx问题总结

    原文网址:http://eminzhang.blog.51cto.com/5292425/1285705 /usr/bin/ld: cannot find -lxxx问题总结   linux下编译应用 ...

  2. Using the Task Parallel Library (TPL) for Events

    Using the Task Parallel Library (TPL) for Events The parallel tasks library was introduced with the ...

  3. HDU 2647

    思路:拓扑排序 #include<stdio.h> #include<string.h> typedef struct { int to; int next; }EdgeNod ...

  4. java命名规范和编程技巧

    一个好的java程序首先命名要规范. 命名规范 定义这个规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,方便维护等作用 Package 的命名 Package 的名字应该都是由一个小写 ...

  5. Spring_Springmvc_mybatis一般配置

    web.xml配置 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=&quo ...

  6. bzoj3998 [TJOI2015]弦论(SAM)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3998 [题意] 询问排名第k的子串是谁,0代表相同子串不同位置算作相同,1代表相同子串 ...

  7. json.net json转换神器

    json.nethttps://json.codeplex.com/ api documenthttp://james.newtonking.com/json/help/index.html#

  8. C++ Primer 练习7.32(C++ Primer读书笔记)

    第七章 类 练习7.32  定义你自己的Screen和Window_mgr,其中clear是Window_mgr的成员,是Screen的友元. 由于Window_mgr中含有Screen对象,所以在W ...

  9. VPN两点注意事项

    今天折腾了半天vpn,特记录以下两点注意事项: 1.客户端VPN连接,点右键属性=>网络选项卡=>双击Internet 协议版本 4 IPV4=>高级=>远程网络上使用默认网关 ...

  10. zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】

    Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...