2017-09-20 12 views
0

私のアプリケーション全体でアクセスを判断するためのカスタムチェックを可能にするカスタム権限属性を作成しました。特定のアクションでオーバーライド/コンバインしないカスタム権限属性

コントローラレベルでカスタム認証属性を適用した後、特定のアクションへのアクセスを追加しようとすると、ロールは「付加的」に適用されません。

は、カスタム属性を承認:コントローラで

// Allow multiple = true so should roll all occurrences in a request into one 
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)] 
public class CustomAuthoriseAttribute : AuthorizeAttribute 
{ 
    public CustomAuthoriseAttribute(params string[] roles) 
    { 
     this.Roles = string.Join(",", roles); 
    } 

    /// <summary> 
    /// Custom routines to determine if a request is considered authorised. 
    /// </summary> 
    /// <param name="httpContext"></param> 
    /// <returns></returns> 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (httpContext == null) 
     { 
      throw new ArgumentNullException("httpContext"); 
     } 

     var userManager = httpContext.GetOwinContext().GetUserManager<UserManager>(); 

     var user = userManager.FindById(httpContext.User.Identity.GetUserId()); 

     if (user == null) 
     { 
      return false; 
     } 

     // Log the user out as they should not be allowed access 
     if (user.IsDisabled || user.IsDeleted) 
     { 
      httpContext.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie); 
      httpContext.Session.Clear(); 

      return false; 
     } 

     return base.AuthorizeCore(httpContext); 
    } 
} 

用途:

がSuperAdminと管理のための認証チェックを打つ、そして自分自身でコンサルタントのチェックを打つことようです不正な要求を引き起こしています。どうして彼らは一体どうして扱われないのですか?

[CustomAuthorise(SuperAdministrator, Administrator)] 
public class SomeController : Controller 
{ 
    public const string SuperAdministrator = "SuperAdministrator"; 
    public const string Administrator = "Administrator"; 
    public const string Consultant = "Consultant"; 

    // Should only accessible by SuperAdministrators and Administrators 
    [HttpGet] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    // Should be accessible by SuperAdministrators, Administrators and Consultants 
    [HttpGet] 
    [CustomAuthorise(Consultant)] 
    public ActionResult SomeAction() 
    { 
     return View(); 
    } 
} 
+0

コードをデバッグし、カスタム属性のどこからfalseを戻していないか確認しましたか? –

+0

これは 'base.AuthorizeCore(httpContext)'を呼び出すときにfalseを返します。つまり、Rolesプロパティの項目のいずれかの 'user.IsInRole'チェックを実行すると失敗するはずです。指定されたロール – Tomuke

+0

baseメソッドを呼び出す前にコード内の 'user.IsInRole'をチェックしてみましたか? –

答えて

0

複数の権限属性は、論理ANDを使用して処理されます。各属性の結果は前のものとANDされます。このシナリオでは、SomeActionは、スーパー・レベルの管理者または管理者(コントローラー・レベルの属性に基づく)のみがアクセスでき、コンサルタント(アクション・レベルの属性に基づいて)にアクセスできます。

これを行うにはいくつかの方法がありますが、制限されたアカウント(コンサルタント)を持つ特権アカウント(スーパー管理者と管理者)を組み合わせる場合は、コンサルタントへのアクセス権をコントローラレベルで付与することをお勧めします。

私は3つの役割すべてからアクセス可能な新しいコントローラを作成し、そこにこのコントローラを移動します。その後、元のコントローラに特権メソッドを残すことができます。

[CustomAuthorise(SuperAdministrator, Administrator)] 
public class PrivilegedController : Controller 
{ 

    // Should only accessible by SuperAdministrators and Administrators 
    [HttpGet] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

} 

[CustomAuthorise(SuperAdministrator, Administrator, Consultant)] 
public class LessPrivilegedController : Controller 
{ 

    [HttpGet] 
    public ActionResult SomeAction() 
    { 
     return View(); 
    } 
} 
関連する問題