can someone please explain to me the main role of claim in .net core in details and how to get information from claim?

276 Views Asked by At

I created a policy and I need to check data in my handler, for example userId and companyId ...

so from the claim principal named user, I need to get information from it so please how can I do that

addscraningHandler :

public class AddScreaningHandler : AuthorizationHandler<AddScreaning>
{
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AddScreaning requirement)
    {
        
        var mvcContext = context.Resource as AuthorizationFilterContext;
        // get user infos
        var claims = context.User.Identities.First().Claims.ToList();
        var userCompanyId = context.User.HasClaim(u => u.Type == "id");
        throw new NotImplementedException();
    }
}

}

this is where I need the policy : controller :

[Authorize(Policy = "AddScreaningHandler")]
        [HttpPost("newScreening")]
        public async Task<ActionResult<Screening>> createScreen(CreateScreeningDto createScreeningDto)
        {
            Screening screening = new Screening { creationDate = DateTime.Now, reflowId=Guid.NewGuid().ToString(), companyId = createScreeningDto.companyId, finished = false, lastUpdate = DateTime.Now, name = "", state = "screening-setup" };
            _context.Screenings.Add(screening);
            await _context.SaveChangesAsync();
            return Ok(screening);
        }

and this is the startup file :

 services.AddAuthorization(options =>
            {
                options.AddPolicy("AddScreaningHandler", policy =>
                    policy.Requirements.Add(new AddScreaning()));
            });
            services.AddSingleton<IAuthorizationHandler, AddScreaningHandler>();
        }

I need to focus on my handler the addscreaninghandler and get the user information, how can I do that .

this is debugging result : enter image description here

enter image description here

0

There are 0 best solutions below