简体   繁体   English

尝试从 header 获取角色声明时,获取 NullReferenceException 和 object 引用未设置为 object 的实例

[英]Getting NullReferenceException and object reference not set to an instance of an object when trying to get the role claim from the header

im trying to get the role claim and verify the it before adding a new user我试图在添加新用户之前获取角色声明并验证它

var role = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Role).Value;

i have already registered service in programs.cs我已经在 programs.cs 中注册了服务

builder.Services.AddHttpContextAccessor();

and initialized well并初始化良好

  private readonly IHttpContextAccessor _httpContextAccessor;

    public DBService(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

Any help would be much appreciated!任何帮助将非常感激!

i have found the answer... this line was looking for a claimtype Role我找到了答案......这一行正在寻找一个 claimtype 角色

var role = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Role).Value;

so i change how claims syntax was written to include the claimtype所以我更改了声明语法的编写方式以包含声明类型

 new Claim(ClaimTypes.Role, role.ToString()),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在获取System.NullReferenceException:尝试在运行时向数组添加值时,对象引用未设置为对象的实例 - I am getting a System.NullReferenceException : Object reference not set to an instance of an object when trying to add a value to an array at runtime 使用ASP.NET 5和MVC 6检查会话是否为空时,获取NullReferenceException和对象引用未设置为对象的实例 - Getting NullReferenceException and object reference not set to an instance of an object when checking if session is null with ASP.NET 5 and MVC 6 获取 System.NullReferenceException:“对象引用未设置为 object 的实例。” 调用服务时 - getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when calling a service NullReferenceException:当用户单击按钮时“未将对象引用设置为对象的实例” - NullReferenceException: “Object reference not set to an instance of an object” when user clicks a button NullreferenceException - 对象引用未设置为对象的实例 - NullreferenceException - Object Reference is not set to an instance of a object 未处理NullReferenceException,未将对象引用设置为对象的实例 - NullReferenceException was unhandled, Object Reference not set to an instance of an object NullReferenceException:对象引用未设置为对象#2的实例 - NullReferenceException: Object reference not set to an instance of an object #2 错误:NullReferenceException:未将对象引用设置为对象的实例 - ERROR: NullReferenceException: Object reference not set to an instance of an object 尝试创建 C# EF 核心迁移时出错,System.NullReferenceException: Object 引用未设置为 object 的实例 - Error when trying to create a C# EF core migration, System.NullReferenceException: Object reference not set to an instance of an object 尝试使用Google AdMob Xamarin Android插入广告时出现“ System.NullReferenceException:对象引用未设置为对象实例” - “System.NullReferenceException: Object reference not set to an instance of an object” when trying to insert ads using google AdMob Xamarin Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM