您可以通过手动设置HttpContext.User来实现此目的:

var identity = new ClaimsIdentity("Custom");

HttpContext.User = new ClaimsPrincipal(identity);

 

对于更复杂的东西,您可以添加如下声明:

var identity = new ClaimsIdentity(new List<Claim>
{
    new Claim("UserId", "123", ClaimValueTypes.Integer32)
}, "Custom");

HttpContext.User = new ClaimsPrincipal(identity);

这将导致:

HttpContext.User.Identity.IsAuthenticated == true;
int.Parse(((ClaimsIdentity)HttpContext.User.Identity).ValueFromType("UserId")) == 123;

 

获取identity信息

        /// <summary>
        /// 当前用户ID
        /// </summary>
        public string UserId
        {
            get
            {
                if (IsAuthenticated && HttpContextAccessor.HttpContext.User.Claims.Any(s => s.Type == ClaimTypes.Name))
                {
                    var result = HttpContextAccessor.HttpContext.User.Claims.SingleOrDefault(s => s.Type == ClaimTypes.Name).Value;
                    return result;
                }
                else
                {
                    return "";
                }
            }
        }


        public IEnumerable<string> RoleKeys
        {
            get {
                if (!string.IsNullOrEmpty(UserId))
                {
                    return HttpContextAccessor.HttpContext.User.Claims.Where(c => c.Type == ClaimTypes.Role)?.Select(c => c.Value)?.ToList();
                }
                else {
                    return new List<string>();
                }
            }
        }

        public IEnumerable<string> RoleIds
        {
            get
            {
                var result=new List<string>();
                if (!string.IsNullOrEmpty(UserId))
                {
                    var roleIds = HttpContextAccessor.HttpContext.User.Claims.SingleOrDefault(c => c.Type == "RoleIds")?.Value;
                    if (!string.IsNullOrEmpty(roleIds))
                    {
                        result= roleIds.Split(',').ToList();
                    }
                }
                return result;
            }
        }

 

 

https://cloud.tencent.com/developer/ask/sof/290519

posted @ 2024-04-18 00:24 BloggerSb 阅读(134) 评论(0) 推荐(0) 编辑
摘要: 在静态类或ServiceProvider来至于静态属性或类已AddSingleton的方式添加进IServiceCollection,此时调用service.GetService<ICoreRepository>(),则会出现Cannot resolve scoped service from ro 阅读全文
posted @ 2024-04-18 00:21 BloggerSb 阅读(426) 评论(0) 推荐(0) 编辑
摘要: MongoDB用户权限管理,设置密码并连接如何设置密码在服务启动状态下,在命令行中输入mongo;首先设置admin表的用户(必须,否则单独设置表用户无用),先执行use admin,再执行db.createUser({user: 'root', pwd: '123456', roles: ['ro 阅读全文
posted @ 2024-04-02 23:00 BloggerSb 阅读(750) 评论(0) 推荐(0) 编辑
摘要: mongodb://localhost:27017 mongodb://root:123456@localhost:27017/testdb?authSource=admin 1、连接本地数据库服务器,端口是默认的。 mongodb://localhost 2、使用用户名fred,密码foobar登 阅读全文
posted @ 2024-04-02 22:52 BloggerSb 阅读(182) 评论(0) 推荐(0) 编辑
摘要: use order 'switched to db order' db.createUser({user:'admin',pwd:'123456',roles:[{role:'dbAdmin',db:'order'}]}) 阅读全文
posted @ 2024-04-02 22:36 BloggerSb 阅读(132) 评论(0) 推荐(0) 编辑
摘要: MongoDB安装包括两部分,一个是将MongoDB的服务下载下来并且安装到自己电脑;一个是可视化界面(解释:你下了MongoDB服务当然要用起来啊,固然你可以使用命令行的客户端,但是建议你安装MongoDB Compass可视化界面,用起来很方便 mongodbhttps://www.mongod 阅读全文
posted @ 2024-04-02 21:34 BloggerSb 阅读(11) 评论(0) 推荐(0) 编辑
摘要: .popover-content { width: 500px; } .popover { max-width:500px; } 阅读全文
posted @ 2024-03-12 15:58 BloggerSb 阅读(60) 评论(0) 推荐(0) 编辑
摘要: <div class="contain" :style="{height: editableHeight + 'px'}" v-html="innerText" ref="editableDiv" contenteditable="true" :placeholder=placeholder @in 阅读全文
posted @ 2024-03-05 22:52 BloggerSb 阅读(264) 评论(0) 推荐(0) 编辑
摘要: 出现这个问题原因: (1)通过打断点可以看到,当你输入的时候触发input事件,提交值给父组件中的v-model; (2)但因为在子组件中又监听了v-model的值,所以整体形成了闭环; (3)还需要重点说明的是光标问题,contenteditable与v-html所在的元素值的改变如果不是通过输入 阅读全文
posted @ 2024-03-03 21:34 BloggerSb 阅读(1676) 评论(0) 推荐(0) 编辑
摘要: 在 C# 中,使用 Flags 属性声明的枚举被称为标志枚举(Flag Enum)。标志枚举允许将多个枚举值组合成一个单独的值,以便同时表示多个状态或选项。 当你使用标志枚举时,可以通过按位运算来组合、拆分和检查枚举值。这使得标志枚举在表示各种组合状态的选项时非常有用。 [Flags] public 阅读全文
posted @ 2024-02-19 17:46 BloggerSb 阅读(240) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示