Home > Back-end >  Getting NullReferenceException and object reference not set to an instance of an object when trying
Getting NullReferenceException and object reference not set to an instance of an object when trying

Time:01-17

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

builder.Services.AddHttpContextAccessor();

and initialized well

  private readonly IHttpContextAccessor _httpContextAccessor;

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

Any help would be much appreciated!

CodePudding user response:

i have found the answer... this line was looking for a claimtype Role

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()),
  • Related