• 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)
                        {
                            //..
                        }
                    }
                }
            }
    
  • 相关阅读:
    导航控制器生产,push,pop,root,index
    DNSserver内置笔记本
    解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
    ssh配置连接
    在UITouch事件中画圆圈-iOS8 Swift基础教程
    iOS之UITableViewCell左右滑动效果
    iOS UIView非常用方法及属性详解
    IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
    UIColor,CGColor,CIColor三者的区别和联系
    iOS中正确的截屏姿势
  • 原文地址:https://www.cnblogs.com/forcheng/p/6795498.html
Copyright © 2020-2023  润新知