ASP.NET Identity 角色管理(Roles)
当我们使用ASP.NET 4.5创建模板项目时,会发现模板只提供了ApplicationUserManager用于用户的登录注册、修改、设置等,而没有提供与用户角色相关的代码,对此就需要我们自己手动的添加。
第一步:在IdentityConfig.cs文件里面添加一个与ApplicationUserManager类似的类:
//定义角色管理器
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>()));
}
}
第二步:在Startup.Auth.cs文件里面创建与ApplicationRoleManager相对应的上下文
public partial class Startup
{
//...
// 有关配置身份验证的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// 将数据库上下文和用户管理器配置为对每个请求使用单个实例
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);//添加角色管理器
//...
}
}
第三步:可以在AccountController类里面封装成属性来使用:
private ApplicationRoleManager _roleManager;
public ApplicationRoleManager RoleManager
{
get { return _roleManager ?? Request.GetOwinContext().Get<ApplicationRoleManager>(); }
set { _roleManager = value; }
}
第四步:使用RoleManager
public async Task SetRole(string userName, string role)
{
var oneRole = await RoleManager.FindByNameAsync(role);
if (oneRole == null)
{
var result = await RoleManager.CreateAsync(new IdentityRole(role));
if (!result.Succeeded)
{
//..
}
}
var user = await UserManager.FindByNameAsync(userName);
if (user != null)
{
var userRoles = UserManager.GetRoles(user.Id);
if (!userRoles.Contains(role))
{
var result = await UserManager.AddToRoleAsync(user.Id, role);
if (!result.Succeeded)
{
//..
}
}
}
}
ASP.NET Identity 角色管理(Roles)的更多相关文章
- Identity角色管理一(准备工作)
因角色管理需要有用户才能进行(需要将用户从角色中添加,删除)故角色管理代码依托用户管理 只需在Startup服务中添加角色管理即可完成 public void ConfigureServices(IS ...
- Identity角色管理二(显示角色)
需要将目前所有角色名显示出来,方法同用户管理 一.创建Index acction public async Task<ActionResult> Index() { var roles = ...
- Identity角色管理五(添加用户到角色组)
因需要在用户列表中点详情按钮来到当前页,所以需要展示分组详情,并展示当前所属角色组的用户 public async Task<ActionResult> Details(string id ...
- Identity角色管理四(删除角色)
角色删除方法 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Delete(string id) ...
- Identity角色管理三(编辑角色)
因只有角色名能修改故继续使用创建角色的视图模型 using System.ComponentModel; using System.ComponentModel.DataAnnotations; na ...
- Identity角色管理三(创建角色)
首先创建视图模型 using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace Shop.Vi ...
- ASP.NET MVC - 安全、身份认证、角色授权和ASP.NET Identity
ASP.NET MVC - 安全.身份认证.角色授权和ASP.NET Identity ASP.NET MVC内置的认证特性 AuthorizeAttribute特性(System.Web.Mvc)( ...
- ASP.NET Identity 使用 RoleManager 进行角色管理 (VS2013RC)
注:本文系作者原创,但可随意转载. 最近做一个Web平台系统,系统包含3个角色,“管理员, 企业用户, 评审专家”, 分别有不同的功能.一直以来都是使用微软封装好的Microsoft.AspNet.I ...
- ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇
在前一篇文章中,我介绍了ASP.NET Identity 基本API的运用并创建了若干用户账号.那么在本篇文章中,我将继续ASP.NET Identity 之旅,向您展示如何运用ASP.NET Ide ...
随机推荐
- 蚁视新家show,小伙伴们快来增加有范儿有爱的蚁视大家庭吧!
蚁视搬新家啦.新家在学院路上,离中关村非常近.离宇宙的中心非常近,离好多同学也非常近有木有! 新家绝对是超级创客范儿,绝对理想中的工作环境有木有!愿意增加蚁视的小伙伴们.快快来吧! 以下是蚁视新办公室 ...
- HTML5客户端数据存储机制Web Storage和Web SQL Database
引言 html5本地存储可以选择两种方式,一种是本地存储,一种是sqlite. 比如开发html5的购物车功能,就可以考虑选择其中之一,进行本地存储与操作. 又或者保存用户登录信息,可以使用local ...
- ora-01157怎么解决
在数据库startup时,出现以下两个错误:ora-01157:cannot identify/lock data file 8 -see DBWR trace fileora-01110:data ...
- 使用Swing组件编写一个支持中文文本编辑程序ChineseTextEdit.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class C ...
- SpringMVC-注解映射器和适配器_20190323
1 注解映射器和适配器 1.1 注解映射器spring3.1之前默认加载映射器是org.springframework.web.servlet.mvc.annotation.DefaultAnnota ...
- jq不懂的地方
在循环列表中,获取input标签的值,不能用id获取,用class获取值,通过父级属性找到class,this 指当前点击的位置var UID = $(this).parents("tr&q ...
- Swift 4.0:访问级别(访问控制)
基础篇 注: 下文中所提及的类和类型为Class, Enum和Struct Swift中的访问级别有以下五种: open: 公开权限, 最高的权限, 可以被其他模块访问, 继承及复写. public: ...
- 线程状态与tcb、线程的生命周期
struct tcb { u32_t status; struct reg_context thread_context; void *stack; struct thread_info thread ...
- sql拼接
with t as( select 'Charles' parent, 'William' child union select 'Charles', 'Harry' union select 'An ...
- 影像服务——加载CESIUM自带的影像服务
1.加载arcgis数据——ArcGisMapServerImageryProvider var viewer = new Cesium.Viewer("cesiumDiv",{ ...