基本环境:asp.net 4.5.2

第一步:在App_Start文件夹中的IdentityConfig.cs中添加角色控制器。

在namespace xxx内(即最后一个“}”前面)添加 角色控制类

代码如下:

//配置此应用程序中使用的应用程序角色管理器。RoleManager 在 ASP.NET Identity 中定义,并由此应用程序使用。
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

在  public void ConfigureAuth(IAppBuilder app) 方法(约为18行左右)中加入  app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

修改完成后的代码如下:

            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);   //添加的角色管理器
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

这里最基本的角色功能启用就完成了。

可选操作:

这个可选操作用于在创建网站的时候,像网站数据库中添加一个管理用户。如果直接发布给别人用的话 还是挺不错的,自己用的话可以省略掉。

第一步:在identityconfig.cs可以配置添加一个用户(用户名为:“admin@123.com”,密码为“Admin@123456”)并把该用户添加到角色("Admin")中。

代码如下:

public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
    {
        protected override void Seed(ApplicationDbContext context) {
            InitializeIdentityForEF(context);
            base.Seed(context);
        }

        //创建用户名为admin@123.com,密码为“Admin@123456”并把该用户添加到角色组"Admin"中
        public static void InitializeIdentityForEF(ApplicationDbContext db) {
            var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
            const string name = "admin@123.com";//用户名
            const string password = "Admin@123456";//密码
            const string roleName = "Admin";//用户要添加到的角色组

            //如果没有Admin用户组则创建该组
            var role = roleManager.FindByName(roleName);
            if (role == null) {
                role = new IdentityRole(roleName);
                var roleresult = roleManager.Create(role);
            }
        
       //如果没有admin@123.com用户则创建该用户
            var user = userManager.FindByName(name);
            if (user == null) {
                user = new ApplicationUser { UserName = name, Email = name };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // 把用户admin@123.com添加到用户组Admin中
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name)) {
                var result = userManager.AddToRole(user.Id, role.Name);
            }
        }
    }

第二步:修改Models文件夹中IdentityModels.cs

在public class ApplicationDbContext : IdentityDbContext<ApplicationUser>   类中添加如下代码

static ApplicationDbContext()
        {
            // 在第一次启动网站时初始化数据库添加管理员用户凭据和admin 角色到数据库            Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        }

asp.net identity 2.2.0 在MVC下的角色启用和基本使用(一)的更多相关文章

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

    角色管理功能: 界面部分: <%@ Page Title="" Language="C#" MasterPageFile="~/Site.mas ...

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

    管理用户功能: 界面部分: <%@ Page Title="" Language="C#" MasterPageFile="~/Site.mas ...

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

    有网友问及权限的问题,其实我觉得没什么改进. 主目录下的web.config基本不用改.要说要改的也就只有数据库连接了. <authentication mode="None" ...

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

    基本环境:asp.net 4.5.2 仔细看了在Webform下,模板就已经启动了角色控制,已经不用再进行设置了.直接调用相关类就可以了.这和原来在网站根目录下配置Web.config完全不同了. 相 ...

  5. asp.net identity 2.2.0 中角色启用和基本使用(七)提示点

    在使用asp.net identity 2.2.0 中,大家可能设计到一些修改和配置 关于Identity的配置,在App_Start文件中的IdentityConfig.cs中,这里几乎有你需要的一 ...

  6. asp.net identity 3.0.0 在MVC下的基本使用 序言

    本人也尚在学习使用之中,错误之处请大家指正. 开发环境:vs2015 UP1   项目环境:asp.net 4.6.1   模板为:asp.net 5 模板     identity版本为:asp.n ...

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

    创建用户管理相关视图 第一步:添加视图   打开UsersAdminController.cs   将鼠标移动到public ActionResult Index()上  右键>添加视图   系 ...

  8. Owin+ASP.NET Identity浅析系列(四)实现用户角色

    在今天,读书有时是件“麻烦”事.它需要你付出时间,付出精力,还要付出一份心境.--仅以<Owin+ASP.NET Identity浅析系列>来祭奠那逝去的…… 通过Owin+ASP.NET ...

  9. asp.net identity 3.0.0 在MVC下的基本使用(一)

    注册时信箱转为用户名. 本人习惯使用用户名做注册用户,因为不管是什么终端起码都能少输入几个字符,可以提高用户体验. 这里需要更改控制器,模型和视图 1.打开Controllers目录下的Account ...

随机推荐

  1. 图片javascript缩小

    function SetSize(obj, width, height) { myImage = new Image(); myImage.src = obj.src; if (myImage.wid ...

  2. chartjs执行图表

    <html> <head> <title></title> </head> <body> <div style='widt ...

  3. QT5.2.1大BUG

    本来以为5.2.1是release版本 谁知道编译某个程序,执行老是crash 换5.3.2就ok了. 坑啊

  4. springMVC 相对于 Structs 的优势

    智者说,没有经过自己的思考和估量,就不能接受别人的东西.资料只能是一个参考,至于是否正确,还得自己去分辨 SpringMVC相对于Structs的几个优势: 1.springMVC安全性更高,stru ...

  5. 一起买beta版PHP单元测试

    一起买beta版PHP单元测试 测试目的 保证代码质量,对各个单元进行测试,可以有效地保证代码的可靠性,让模块在与别的模块整合时出现更少的错误. 单元描述 完成帖子接口 ​ 将"正在进行&q ...

  6. cocos2dx的build_win32.dat出现问题以及install-template-msvc.dat出现.js没有脚本引擎

    关于cocos2dx-2.x.x版本当中出现build_win32.bat执行失败 (针对VS2013)应当在VS的安装路径查找msbuild的文件夹,再其中查找msbuild.exe文件找到四个东西 ...

  7. MvcPager 免费开源分页控件3.0版发布!

    MvcPager 3.0版在原2.0版的基础上进行了较大的升级,对MvcPager脚本插件重写并进行了大量优化.修复了部分bug并新增了客户端Javascript API等功能,使用更方便,功能更强大 ...

  8. sqlserver2012 表分区

    无论是新建数据库,还是现有的问题,都可以执行表分区的操作. 1.在数据库中点鼠标右键点击属性,在选择页,选中文件栏,在数据库文件列表中,可以看到现有的数据库文件逻辑名称.文件类型.初始大小.保存位置等 ...

  9. [转]SQL Server字符串处理函数大全

    select语句中只能使用sql函数对字段进行操作(链接sql server), select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不 ...

  10. 基于ArcGIS Viewer for Flex开发的一款跨平台的应用程序

    特点: 1.基于ArcGIS Viewer for Flex开发的一款跨平台的应用程序: -(IBAction) showTOC:(id)sender { if (_tocViewController ...