【发布时间】:2015-02-28 03:09:26
【问题描述】:
我有以下 WebApi 操作,它从后端数据库中删除订单,仅适用于 Admin 和 Order 角色中的用户。但是,如果用户也是Readonly 角色,则操作将返回HTTP 403 Forbidden 响应。
[Authorize(Roles = "Admin,Order")]
public async Task<IHttpActionResult> Delete(int orderid) {
if(User.IsInRole("Readonly")) { return Forbidden(); }
var order = await _repository.Get(orderid);
if(order != null) {
await _repository.Delete(orderid);
return NoContent();
}
else {
return NotFound();
}
}
我想知道的是,如果用户处于特定角色中,是否可以阻止执行操作,这样我就不必在所有数据库可更新操作方法的开头放置if(User.IsInRole("Readonly")) { return Forbidden(); },例如
[Authorize(Roles = "Admin,Order")]
[NotAuthorized(Roles = "Readonly")]
public async Task<IHttpActionResult> Delete(int orderid) {
var order = await _repository.Get(orderid);
if(order != null) {
await _repository.Delete(orderid);
return NoContent();
}
else {
return NotFound();
}
}
如果用户处于Readonly 角色中,NotAuthorized 操作过滤器将返回 HTTP 403 Forbidden 响应。
这可能吗?
【问题讨论】:
标签: asp.net-web-api