Hello!
I am using policy based authorization. Here is what I have in my Startup.cs:
services.AddSingleton<IAuthorizationHandler, PrivateAuthorizationHandler>(); services.AddAuthorization(options => { options.AddPolicy("User", policy => policy.Requirements.Add(new PrivateRequirement(new[] {1}, "User"))); options.AddPolicy("Super User", policy => policy.Requirements.Add(new PrivateRequirement(new[] {1, 2}, "Super User"))); options.AddPolicy("Admin", policy => policy.Requirements.Add(new PrivateRequirement(new[] {1, 2, 3}, "Admin"))); });
My handler looks like that:
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PrivateRequirement requirement) { try { var ntName = context.User.Identity.Name; // <= always empty string "" if (string.IsNullOrWhiteSpace(ntName)) { context.Fail(); // <= always getting here } else { //check requirement } } catch (Exception ex) { context.Fail(); } return Task.CompletedTask; }
I am using Windows Authentication. Locally it is working fine. When I deploy the project to IIS, I am getting the issue above. It is like context is not being passed to the handler correctly.
Any idea on how to get Authenticated User in a handler?
Thank you!