2017-12-19 8 views
0

FluentSecurityを使用して権限を設定するASP.NET MVC Webサイトで作業しています。現在、カスタムアクションリンクヘルパーが必要です。これは、現在のユーザーがターゲットとするアクションにアクセスできる場合にのみ表示されます。FluentSecurity設定から動的に情報を抽出します

FluentSecurity Configuration(たとえばSecurityConfigurationクラスを使用)から、現在のログインユーザーが自分の名前(文字列)とコントローラ名(文字列)を指定したアクションにアクセスできる場合、動的に知る方法があるかどうかを知りたい)。 FluentSecurity https://github.com/kristofferahl/FluentSecurityのソースコードを調べるのに多くの時間を費やしますが、成功することはありません。例えば

public bool HasAccess(string controllerName, string actionName) { 
     //code I'm looking for goes here 
} 

答えて

0

最後に、私は、これは別のものを助けることが自分自身にお答えします。

HandleSecurityAttributeクラスのOnAuthorizationメソッドをエミュレートするだけです。このコードはうまくいきます:

public static bool HasAccess(string fullControllerName, string actionName) 
    { 
     ISecurityContext contx = SecurityContext.Current; 
     contx.Data.RouteValues = new RouteValueDictionary(); 

     var handler = new SecurityHandler(); 

     try 
     { 
      var result = handler.HandleSecurityFor(fullControllerName, actionName, contx); 
      return (result == null); 
     } catch (PolicyViolationException) 
     { 
      return false; 
     } 

    } 
関連する問題