![](/img/trans.png)
[英]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
[英]Getting NullReferenceException and object reference not set to an instance of an object when trying to get the role claim from the header
我试图在添加新用户之前获取角色声明并验证它
var role = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Role).Value;
我已经在 programs.cs 中注册了服务
builder.Services.AddHttpContextAccessor();
并初始化良好
private readonly IHttpContextAccessor _httpContextAccessor;
public DBService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
任何帮助将非常感激!
我找到了答案......这一行正在寻找一个 claimtype 角色
var role = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Role).Value;
所以我更改了声明语法的编写方式以包含声明类型
new Claim(ClaimTypes.Role, role.ToString()),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.