Skip to content

Commit

Permalink
Update authz
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Apr 15, 2024
1 parent fbb026d commit c9ea7f0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion dry/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using dry.Server;
using dry.Server.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
Expand Down Expand Up @@ -41,7 +42,7 @@
services.AddControllersWithViews(options =>
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()));

services.AddSingleton<UserAdminHandler>();
services.AddSingleton<IAuthorizationHandler, UserAdminHandler>();

services.AddAuthorization(options =>
{
Expand Down
6 changes: 3 additions & 3 deletions dry/Server/UserAdminHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public class UserAdminHandler : AuthorizationHandler<UserAdminRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserAdminRequirement requirement)
{
var userClaim = context.User.FindFirst(c => c.Type == "roles" && c.Value == "user");
var adminClaim = context.User.FindFirst(c => c.Type == "roles" && c.Value == "admin");
var userClaim = context.User.HasClaim(c => c.Type == "roles" && c.Value == "user");
var adminClaim = context.User.HasClaim(c => c.Type == "roles" && c.Value == "admin");

if (userClaim is null && adminClaim is null)
if (userClaim && adminClaim)
{
return Task.CompletedTask;
}
Expand Down

0 comments on commit c9ea7f0

Please sign in to comment.