【问题标题】:c# WebApi Attribute Authorizationc# WebApi 属性授权
【发布时间】:2016-04-27 10:05:39
【问题描述】:

我有一个外部程序员编写的代码。该项目是一个WebApi。我不知道授权是如何发生的。我认为,我的控制器方法上方的一个属性使这种情况发生。但我不明白授权是如何发生的。控制器方法示例:

    [HttpGet]
    [Route("organizationunits/{entity}/{type}")]
    [MDLAuthorize(Actions.Read)]
    public async Task<IHttpActionResult> GetEntities(string entity, string type)
    {//some code}

MDLAuthorize 属性指定一种方法。而且我认为以某种方式调用了 IsAuthorized 方法。

public class MDLAuthorize : AuthorizeAttribute
{
    private string _action;

    public MDLAuthorize(string action)
        : base()
    {
        _action = action;
    }

    protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        try
        {
            if (String.IsNullOrEmpty(_action))
                return false;

            var baseAuthorized = base.IsAuthorized(actionContext);

            string activity = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            if (actionContext.RequestContext.Principal == null ||
                actionContext.RequestContext.Principal.Identity == null)
            {
                //no principal, no fun.
                return false;
            }
            else
            {
                string username = actionContext.RequestContext.Principal.Identity.Name;
                bool isAuthorized = Security.HasPermission(username, activity, _action);
                return isAuthorized;
            }
        }
        catch (Exception ex)
        {
            MDLApiLog.Error(ex);
            return false;
        }
    }
}

我不知道我的问题是否需要,但这是 AuthorizeAttribute 类

    //
// Summary:
//     Specifies the authorization filter that verifies the request's System.Security.Principal.IPrincipal.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeAttribute : AuthorizationFilterAttribute
{
    //
    // Summary:
    //     Initializes a new instance of the System.Web.Http.AuthorizeAttribute class.
    public AuthorizeAttribute();

    //
    // Summary:
    //     Gets or sets the authorized roles.
    //
    // Returns:
    //     The roles string.
    public string Roles { get; set; }
    //
    // Summary:
    //     Gets a unique identifier for this attribute.
    //
    // Returns:
    //     A unique identifier for this attribute.
    public override object TypeId { get; }
    //
    // Summary:
    //     Gets or sets the authorized users.
    //
    // Returns:
    //     The users string.
    public string Users { get; set; }

    //
    // Summary:
    //     Calls when an action is being authorized.
    //
    // Parameters:
    //   actionContext:
    //     The context.
    //
    // Exceptions:
    //   T:System.ArgumentNullException:
    //     The context parameter is null.
    public override void OnAuthorization(HttpActionContext actionContext);
    //
    // Summary:
    //     Processes requests that fail authorization.
    //
    // Parameters:
    //   actionContext:
    //     The context.
    protected virtual void HandleUnauthorizedRequest(HttpActionContext actionContext);
    //
    // Summary:
    //     Indicates whether the specified control is authorized.
    //
    // Parameters:
    //   actionContext:
    //     The context.
    //
    // Returns:
    //     true if the control is authorized; otherwise, false.
    protected virtual bool IsAuthorized(HttpActionContext actionContext);
}

【问题讨论】:

  • 是的,Authorize 属性导致方法被调用。如果用户只被授权,那么对 webapi 方法的调用将被执行,否则 HTTP 未经授权的代码将被返回给客户端

标签: c# asp.net-web-api


【解决方案1】:

任何继承AuthorizeAttribute 的属性都会在请求时调用其IsAuthorized() 方法。派生属性中该方法的主体使用Security.HasPermission() 方法检查用户是否能够执行该操作。

【讨论】:

  • 感谢您的回答。为什么会自动调用“IsAuthorized()”方法。您的意思是在调用 Authorize 属性时调用它吗?
  • AuthorizeAttribute 本身派生自 AuthorizationFilter。这些过滤器在 MVC 中的请求时调用 - 它只是框架固有的。
  • 好吧,我想我会研究 MVC ;)。感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 2012-05-09
  • 1970-01-01
  • 2016-05-14
  • 2013-03-28
  • 2016-01-07
  • 2017-04-05
相关资源
最近更新 更多