0

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

Merna Mustafa
  • 1,235
  • 2
  • 10
  • 22
junior
  • 1
  • 5
  • Your second image already shows the available claims and their keys and values. What exactly is your problem? Are you missing some data or are unable to read it or something else? You can see a general example e.g. here: https://stackoverflow.com/questions/30701006/how-to-get-the-current-logged-in-user-id-in-asp-net-core – Christoph Lütjen Nov 11 '21 at 11:39
  • yes i need to get the user information from it and i dont know how to do it, i need the user id and the userCompanyId – junior Nov 11 '21 at 11:46
  • This is answered with example in the linked question. Why is this answer not working for you? – Christoph Lütjen Nov 11 '21 at 11:59
  • ok so with the keys and values of the claims i can get the user informations ? how can i do that ? i'm sorry i'm beginner – junior Nov 11 '21 at 12:05
  • Did you read the answer? If you look in the controller class there's a line `var userId = User.FindFirstValue(ClaimTypes.NameIdentifier) // will give the user's userId`. – Christoph Lütjen Nov 11 '21 at 12:13
  • when testing on claim i need to get the informations of the current user to do some logic and tests , that exactly what i need , thanks . – junior Nov 11 '21 at 12:14
  • These "modern ways" are a little bit tricky from time to time ;-) – Christoph Lütjen Nov 11 '21 at 12:17

0 Answers0