在某些控制器中,我想根据公司ID授权用户 .
例如,请考虑以下资源:
api/v1/companies/1234/orders
只有属于公司1234的用户才能访问此信息 .
请注意,我正在使用OAuth令牌承载身份验证 .
Creating the company claim in OAuthAuthorizationServerProvider
:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
//...
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
// Creating the companyId claim
identity.AddClaim(new Claim("CompanyId", user.CompanyId.ToString()));
context.Validated(identity);
}
订单资源的当前 controller 实现:
[RoutePrefix("api/v1/companies/{companyId:Guid}/orders")]
public class OrdersController : ApiController
{
[Route]
public IHttpActionResult GetOrders(Guid companyId)
{
var orders = OrdersRepository.Get(companyId);
return Ok(orders.Select(x => OrderModel.From(x)));
}
}
Where do I authorize the companyId URL value against the identity claim?
可以 [Authorize]
以某种方式在这里使用?
2 回答
这是您可以使用的自定义授权过滤器
如果您只希望一家公司访问某个操作或控制器,您也可以使用下面的操作或控制器进行装饰 .
请参阅https://github.com/AzureADSamples/NativeClient-DotNet/blob/master/TodoListService/Controllers/TodoListController.cs中的方法 - 而不是"scope",您可以使用自定义声明类型