2016-11-29 4 views
2

私はカスタムのAuthorizeAttributeを作成して、ユーザーが特定のリソースにアクセスできるかどうかをチェックしました。私は簡単にリソースタイプが要求された属性を伝えることができカスタム属性へのルートパラメータの受け渡し

  1. 要求されたリソースの種類
  2. 要求されたリソースID

:それは一般的なようにするために、私は、属性に渡された2つの変数が必要要求されたIDを属性に渡す方法はありますか?ここに私のコードは、(?でマークされた)欠落しているだけで、最後の変数で、次のとおりです。

[System.Web.Http.Authorize] 
[System.Web.Http.RoutePrefix("api/accounts")] 
public class AccountController : ApiController 
{ 
    [AuthorizeIsOwnResource(ResourcesType = ResourcesTypes.Account, ResourceId = ?)] 
    [System.Web.Http.HttpGet] 
    [System.Web.Http.Route("test/{id}")] 
    public ActionResult Test(string id) 
    { 
     return new HttpStatusCodeResult(HttpStatusCode.OK); 
    } 
} 

任意の提案?私は属性パラメータを一括して取り除くことができ、AuthorizeCoreHttpContextBaseから要求されたリソースタイプとIDをデシフェートできると思います。それは私の唯一の選択肢ですか?

+0

actionContext.ControllerContext.RequestContext.RouteData;から要求されたIDを取得します。私は最高の解決策は、HttpContextBaseからコントローラ名とアクション名にアクセスすることだと思います。よりクリーンで、実装が簡単で、エラーが発生しにくいです。 –

+0

問題について少し熟考した後、私はあなたが絶対に正しいと思います。私はHttpContextBaseからRouteDataを抽出することさえできるかもしれないと思う。私はそれをテストした後、私は解決策を投稿します – MichaelCleverly

答えて

2

よし。 Douglas Gandiniのコメントに基づいて、どのIDが要求されたかを決定するために属性に任せておくのが最善の方法であると判断しました。要するに

public class AuthorizeIsOwnResourceAttribute : AuthorizeAttribute 
{ 
    public ResourcesTypes ResourcesType { get; set; } 

    protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext) 
    { 
     var httpContext = HttpContext.Current; 
     var claimsIdentity = httpContext.User.Identity as ClaimsIdentity; 
     var routeData = actionContext.ControllerContext.RequestContext.RouteData; 

     switch (ResourcesType) 
     { 
      case ResourcesTypes.Account: 
       return AuthorizeAccount(routeData, claimsIdentity); 
     } 

     return false; 
    } 

    private bool AuthorizeAccount(IHttpRouteData routedata, ClaimsIdentity claimsIdentity) 
    { 

     var id = routedata.Values["id"].ToString(); 
     var accountClaim = claimsIdentity.Claims.FirstOrDefault(x => x.Type == "Resource-" + ResourcesTypes.Account); 
     if (accountClaim == null || accountClaim.Value != id) 
     { 
      return false; 
     } 

     return true; 
    } 
} 

:ここに私の作業カスタム属性である私は、あなたが答えを持って、直接私の属性

0

あなたAuthorizationContextのコードでActionDescriptorをオーバーライドすることができます:

// Summary: 
    //  Initializes a new instance of the System.Web.Mvc.AuthorizationContext class using 
    //  the specified controller context and action descriptor. 
    // 
    // Parameters: 
    // controllerContext: 
    //  The context in which the result is executed. The context information includes 
    //  the controller, HTTP content, request context, and route data. 
    // 
    // actionDescriptor: 
    //  An object that provides information about an action method, such as its name, 
    //  controller, parameters, attributes, and filters. 
    public AuthorizationContext(ControllerContext controllerContext, ActionDescriptor actionDescriptor); 


    // Summary: 
    //  Provides information about the action method that is marked by the System.Web.Mvc.AuthorizeAttribute 
    //  attribute, such as its name, controller, parameters, attributes, and filters. 
    // 
    // Returns: 
    //  The action descriptor for the action method that is marked by the System.Web.Mvc.AuthorizeAttribute 
    //  attribute. 
    public virtual ActionDescriptor ActionDescriptor { get; set; } 
    // 
    // Summary: 
    //  Gets or sets the result that is returned by an action method. 
    // 
    // Returns: 
    //  The result that is returned by an action method. 
    public ActionResult Result { get; set; } 
関連する問題