在 .net core web api 中实现基于用户的授权过滤器

时间:2021-01-12 04:02:40

标签: c# asp.net-core-webapi

我正在 CleanArchitecture 上开发一个简单的 WebApi .Net Core CRUD,将实现以下安全方面。 (1) Authentication -Token-Based Jwt (2) 授权: 如何使用授权过滤器实现基于用户的授权场景,例如“授权用户只能更改其联系人”? 我可以在操作级别执行以下操作,但想使用过滤器!

[Authorize]
[HttpPut("{userName}")]
public async Task<IActionResult> UpdateContact(string userName, UpdateContactCommand command)
{
  if (User.Identity.Name == userName)
      return Ok(await _mediator.Send(command));
}

0 个答案:

没有答案