c# - 如何配置注解 [Authorize] 以使用枚举?

标签 c# asp.net-mvc asp.net-mvc-4

我正在尝试使用 Enum 来注释 [Authorize]。我已经配置,但它仍然不起作用,安全策略允许访问。

我没有用数字创建我的 Enum 例如:Administrator = 1 或类似的东西,我只是使用 description as Administrator, Manager, Common< 创建。我不会创建数字作为索引,只是想按照我显示的描述来创建。

我该如何解决这个问题?

枚举

public enum RoleType{
    Administrator,
    Manager,
    Common
};

AuthorizeAttribute

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Enum | AttributeTargets.Method, AllowMultiple = false)]
public class PermissionFilter : AuthorizeAttribute{

    public RoleType Roles {get;set;}

    protected override bool AuthorizeCore(HttpContextBase httpContext){
        if (httpContext == null)
            throw new ArgumentNullException("httpContext");

        if (!httpContext.User.Identity.IsAuthenticated)
            return false;

        //get the Session of User
        User user = httpContext.Session["User"] as User;
        RoleType role = user.role;

        if (((Roles & role) != role))
            return false;

        return true;
    }

    public override void OnAuthorization(AuthorizationContext filterContext){
        base.OnAuthorization(filterContext);

        if (filterContext.Result is HttpUnauthorizedResult)
            filterContext.HttpContext.Response.Redirect("/Home/accessDenied");
    }
}

方法

[PermissionFilter(Roles= RoleType.Manager)]
public ActionResult viewAllAdmin(int? pagina, String nome){
}

最佳答案

问题解决了。

我做了

AuthorizeAttribute

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Enum | AttributeTargets.Method, AllowMultiple = false)]
public class PermissionFilter : AuthorizeAttribute{

    public RoleType[] Roles;

    public PermissionFilter(params RoleType[] roles){
        Roles = roles;
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext){
        if (httpContext == null)
            throw new ArgumentNullException("httpContext");

        if (!httpContext.User.Identity.IsAuthenticated)
            return false;
        try{
            Usuario usuario = httpContext.Session["Usuario"] as Usuario;
            RoleType role = usuario.role;
            Boolean contain = Roles.Contains<RoleType>((RoleType)role);
            Console.WriteLine("Contem Role: " + contain);

            if (!Roles.Contains<RoleType>((RoleType)role)){
                return false;
            }

            return true;
        }catch (Exception e){
            Debug.WriteLine("PermissionFilter AuthorizeCore: " + e.Message);
            return false;
        }       
    }


    public override void OnAuthorization(AuthorizationContext filterContext){
        base.OnAuthorization(filterContext);

        if (filterContext.Result is HttpUnauthorizedResult)
            filterContext.HttpContext.Response.Redirect("/Home/acessoNegado");
    }
}

方法

[PermissionFilter(RoleType.Administrator, RoleType.Manager)]
public ActionResult viewAllAdmin(int? pagina, String nome){
}

然后,它工作正常!

关于c# - 如何配置注解 [Authorize] 以使用枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46043617/

相关文章:

asp.net-mvc-4 - 带 ID 的 Html.Actionlink

ASP.NET MVC 4 AJAX 提交表单不起作用

c# - SubSonic 无法识别 SQLite 外键

c# - 动态添加的 LinkBut​​ton CommandName/CommandArgument 设置被删除

asp.net-mvc - 通用代码的 mvc 最佳实践

asp.net - 将 VS2015 中的 ASP.NET Identity 中的 User Id 类型更改为 int

c# - 将用户控件从解决方案资源管理器添加到 winform

c# - 在构建项目引用和包期间正在安装和引用不同版本的 nuget 包

asp.net-mvc - ASP.Net MVC - HTTP 状态代码(即 303、401、404 等)

c# - MVC 4 _Layout.cshtml 动态 CSS