在ASP.NET Core 1应用程序的逻辑层中获取ClaimsPrincipal [英] Getting the ClaimsPrincipal in a logic layer in an aspnet core 1 application

查看:56
本文介绍了在ASP.NET Core 1应用程序的逻辑层中获取ClaimsPrincipal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个aspnet core 1应用程序.使用承载令牌身份验证,我在控制器内部拥有具有正确身份的User属性.但是,我似乎找不到像使用 ClaimPrincipal.Current 静态方法之前那样获取身份的方法.在不传递ClaimPrincipal对象的情况下,如何在BL层中获取此数据的当前最佳实践是什么?

I'm writing an aspnet core 1 application. Using a bearer token authentication I have the User property inside the controller with the correct identity. However I cant seem to find a way to grab with identity as I did before using the ClaimPrincipal.Current static. What is the current best practice to get this data inside the BL layers with out passing the ClaimPrincipal object around?

推荐答案

进一步研究此问题,我发现可以使用本机DI容器在需要的地方注入 ClaimsPrincipal :

Further investigating this issue I've found that it is possible to use the native DI container to inject the ClaimsPrincipal where needed like that:

services.AddTransient<ClaimsPrincipal>(s =>
    s.GetService<IHttpContextAccessor>().HttpContext.User);

注入它感觉有点奇怪,但是比将其存储在 CallContext 中更好.

This feels kind of weird injecting it, however it is better than storing it in the CallContext.

这篇关于在ASP.NET Core 1应用程序的逻辑层中获取ClaimsPrincipal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆