[英]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.