【问题标题】:Forbid users from executing WebApi actions禁止用户执行 WebApi 操作
【发布时间】:2015-02-28 03:09:26
【问题描述】:

我有以下 WebApi 操作,它从后端数据库中删除订单,仅适用于 AdminOrder 角色中的用户。但是,如果用户也是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


    【解决方案1】:

    这是实现 [Authorize()] 属性的反向代码并禁止用户执行 MVC WebApi 操作(如果他们是一个或多个角色的成员)。

    using System;
    using System.Net;
    using System.Net.Http;
    using System.Security.Principal;
    using System.Web.Http;
    using System.Web.Http.Controllers;
    
    namespace MyAPI {
    
        [AttributeUsage(AttributeTargets.Method,AllowMultiple = false)]
        public class NotAuthorizedAttribute : AuthorizeAttribute {
    
            public override void OnAuthorization(HttpActionContext actionContext) {
                IPrincipal user = actionContext.RequestContext.Principal;
                if(!user.Identity.IsAuthenticated) {
                    actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                }
                else {
                    bool userInRole = false;
                    foreach(var role in Roles.Split(',')) {
                        if(user.IsInRole(role)) {
                            userInRole = true;
                            break;
                        }
                    }
                    if(userInRole) {
                        actionContext.Response = new HttpResponseMessage(HttpStatusCode.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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多