具有自定义需求/处理程序的.Net核心授权

时间:2019-02-20 15:29:59

标签: asp.net-core .net-core asp.net-core-mvc .net-core-authorization

我之前曾问过类似的问题,并在别人的帮助下取得了一些进展,但仍然不确定我在这里做错了什么或错过了什么。

我的应用程序很简单: 域用户得到认证。 经过身份验证的用户(作者)创建一个请求,保存在数据库中。 其他经过身份验证的用户只能查看该请求。 作者和管理员用户可以编辑/删除请求。

这是我遵循的示例之一: Different API functionality for different roles

另外一个是PolicyAuthApp:https://github.com/aspnet/Docs/tree/master/aspnetcore/security/authorization/policies/samples/PoliciesAuthApp1 我不确定在这里如何使用/注册/调用PermissionHandler。

这是我的代码:

Startup.cs

//  Add Authentication
//  Global filter
services.AddMvc(config =>
{
    var policy = new AuthorizationPolicyBuilder()
                     .RequireAuthenticatedUser()
                     .RequireRole("Role - Domain Users")
                     .Build();
    config.Filters.Add(new AuthorizeFilter(policy));
});


//  Add Authorization Handlers
services.AddAuthorization(options =>
{
    options.AddPolicy("EditPolicy", policy => policy.Requirements.Add(new EditRequirement()));
});

services.AddSingleton<IAuthorizationHandler, PermissionHandler>();

EditRequirement.cs

public class EditRequirement : IAuthorizationRequirement
{
}

PermissionHandler.cs

public class PermissionHandler : IAuthorizationHandler
{

    public Task HandleAsync(AuthorizationHandlerContext context)
    {
        var pendingRequirements = context.PendingRequirements.ToList();

        foreach (var requirement in pendingRequirements)
        {
            if (requirement is ReadRequirement)
            {
                if (IsOwner(context.User, context.Resource) ||
                    IsAdmin(context.User, context.Resource))
                {
                    context.Succeed(requirement);
                }
            }
            else if (requirement is EditRequirement ||
                     requirement is DeleteRequirement)
            {
                if (IsOwner(context.User, context.Resource) || IsAdmin(context.User, context.Resource))
                {
                    context.Succeed(requirement);
                }
            }
        }
        return Task.CompletedTask;
    }

    private bool IsAdmin(ClaimsPrincipal user, object resource)
    {
        if (user.IsInRole("Role - Administrator"))
        {
            return true;
        }
        return false;
    }

    private bool IsOwner(ClaimsPrincipal user, object resource)
    {
        if (resource is CreateRequestViewModel)
        {
            var ctx = (CreateRequestViewModel)resource;

            if (ctx.RequestEnteredBy.Equals(user.Identity.Name))
            {
                return true;
            }
        }
        else if (resource is AuthorizationFilterContext)
        {
            var afc = (AuthorizationFilterContext)resource;

            //  This is not right, but I don't know how to deal with AuthorizationFilterContext 
            //  type passed into resource parameter when I click Edit button trying to edit the request
            if (afc.HttpContext.User.Identity.Name.Equals(user.Identity.Name))
            {
                return true;
            }
        }
        else if (resource is Request)
        {
            var r = (Request)resource;

            if (r.RequestEnteredBy.Equals(user.Identity.Name))
            {
                return true;
            }
        }


        return false;
    }

    private bool IsSponsor(ClaimsPrincipal user, object resource)
    {
        // Code omitted for brevity

        return true;
    }
}

RequestsController.cs

private IAuthorizationService _authorizationService;

public RequestsController(ApplicationModelContext context, IAuthorizationService authorizationService)
{
    _context = context; 
    _authorizationService = authorizationService;
}

[Authorize(Policy = "EditPolicy")]
public async Task<IActionResult> Edit(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    CreateRequestViewModel crvm = new CreateRequestViewModel();

    var request = await _context.Request
                .SingleOrDefaultAsync(m => m.RequestId == id);

    if (request == null)
    {
        return NotFound();
    }

    var authorizationResult = await _authorizationService.AuthorizeAsync(User, request, "EditPolicy");

    if (authorizationResult.Succeeded)
    {   
        //  Load request contents and return to the view

        return View(crvm);
    }

    //  This needs to be changed to redirect to a message screen saying no permission
    return RedirectToAction("Details", new { id = request.RequestId });
}

在调试应用程序时,我发现:

  • 页面第一次加载来自数据库的请求时,它会通过 资源参数的CreateRequestViewModel类型对象 PermissionHandler中的IsOwner方法。
  • 在页面上单击请求项的“编辑”按钮后,它通过 AuthorizationFilterContext类型
  • 然后进入ReuqestController中的Edit动作,该动作将发送一个 请求类型(在_authorizationService.AuthorizeAsync中)。

不确定我是否做过重复或完全混合使用不同方法的事情。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您似乎正在混合使用基于策略和资源的授权。基于资源的声音听起来像您想要的,因为您可能不想为每个CRUD操作创建策略,即“ CreateUserPolicy”,“ UpdateUserPolicy”,然后将不同的要求传递给每个。 有关基于资源的授权,请参见本教程:https://docs.microsoft.com/en-us/aspnet/core/security/authorization/resourcebased?view=aspnetcore-2.2

为了针对用户资源进行授权,我创建了一个UserAuthorizationHandler:

Startup.cs:

services.AddScoped<IAuthorizationHandler, UserAuthorizationHandler>();

UserAuthorizationHandler.cs

public class UserAuthorizationHandler : AuthorizationHandler<OperationAuthorizationRequirement, User>
{
    private readonly IPermissionRepository _permissionRepository;

    public UserAuthorizationHandler(IPermissionRepository permissionRepository)
    {
        _permissionRepository = permissionRepository;
    }

    protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, OperationAuthorizationRequirement requirement, User resource)
    {
        var authUserId = int.Parse(context.User.FindFirstValue(ClaimTypes.NameIdentifier));

        if (requirement == AuthorizationOperations.Create)
        {
            if (await CanCreate(authUserId))
            {
                context.Succeed(requirement);
            }
        }
        else if (requirement == AuthorizationOperations.Read)
        {
            if (await CanRead(authUserId, resource))
            {
                context.Succeed(requirement);
            }
        }
    }

    /// <summary>
    /// User can create if they have 'create' 'users' permission.
    /// </summary>
    /// <param name="authUserId">The requesting user</param>
    /// <returns></returns>
    public async Task<bool> CanCreate(int authUserId)
    {
        return await _permissionRepository.UserHasPermission(authUserId, "create", "users");
    }

    /// <summary>
    /// User can read if reading themselves or they have the 'read' 'users' permission.
    /// </summary>
    /// <param name="authUserId">The requesting user</param>
    /// <param name="user">The requested resource</param>
    /// <returns></returns>
    public async Task<bool> CanRead(int authUserId, User user)
    {
        return authUserId == user.Id || await _permissionRepository.UserHasPermission(authUserId, "read", "users");
    }
}

控制器:

  var authorized = await _authorizationService.AuthorizeAsync(User, new User { Id = id}, AuthorizationOperations.Read);
  if (!authorized.Succeeded)
  {
    return Unauthorized();
  }