【问题标题】:getting Roles that already set to custom Authorization attribute?获取已设置为自定义授权属性的角色?
【发布时间】:2015-06-13 15:20:53
【问题描述】:

我自定义了 Asp.Net 的 authorize 属性,但是当我将属性设置为方法时,我不知道如何获取我设置为属性的角色或班级

例如我有这个 CustomeAuthorizeAttribute

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class CustomeAuthorizeAttribute : AuthorizeAttribute
{

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {


        if (HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.IsInRole("Super"))
        {
            return true;
        }
        else
            return false;
    }
}

但是当我将角色设置为这样的属性时,我不知道如何获取角色

[CustomeAuthorizeAttribute(Roles="admin,super-admin")]

【问题讨论】:

    标签: asp.net asp.net-authorization asp.net-authentication custom-authentication


    【解决方案1】:

    默认情况下,它从基础 Authorize 类中继承 Roles 属性,因此您可以使用 Roles 属性直接获取角色

    举例

    if (HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.IsInRole(Roles))
        {
            return true;
        }
    

    或者您创建属于您的自定义授权属性的新属性并使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      相关资源
      最近更新 更多