c# - 如果我所有的 .Net Controllers/Action 都需要 Authorize Attr,为什么没有一个属性只使用那些不需要的?

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

我有一个应用程序需要授权才能访问所有 Controller /操作。除了登录错误 Controller /操作

在这种情况下,以防御方式工作最好保持默认限制对所有 Controller /操作的访问(没有授权属性),并只选择那些没有授权的自定义属性。

你们做过这样的事吗?

我有一个 MVC 过滤器,如果登录用户可以访问它们,它会在所有操作之前执行:

public class ValidatePermissionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        bool isAuthorized = false;

        //Logic that verify if logged user have permission to access the requested Controller/Action
        ...

        //Redirect to a page Error if Logged User don't have Authorization
        if (!isAuthorized)
        {
            RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
            redirectTargetDictionary.Add("action", "Erro");
            redirectTargetDictionary.Add("controller", "Index");

            context.Result = new RedirectToRouteResult(redirectTargetDictionary);
        }
    }
}

我正在考虑执行此操作的最佳方法。我可以创建一个空白自定义属性并放入不需要授权的 Controller 并在我的过滤器中检查它:

public class ValidatePermissionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        bool isAuthorized = false;

        var DoNotRequiresAuthorizationAttributes = context.ActionDescriptor.GetCustomAttributes(typeof(DoNotRequiresAuthorizationAttribute), false);

        if (DoNotRequiresAuthorizationAttributes.Length > 0)
            isAuthorized = true;

        ...

        //Redirect to a page Error if Logged User don't have Authorization
        if (!isAuthorized)
        {
            RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
            redirectTargetDictionary.Add("action", "Erro");
            redirectTargetDictionary.Add("controller", "Index");

            context.Result = new RedirectToRouteResult(redirectTargetDictionary);
        }
    }
}

专家们怎么看?

更新:

更好地思考,我可以用自定义授权属性替换我的过滤器,并注册它以在中的所有 Controller /操作中执行>Global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new MyCustomAuthorizeAttribute());
}

更新 2:

而是创建一个空白自定义属性并放入 Controller 中不需要授权我传入自定义参数授权 Controller 不需要授权(在 Global.asax 中):

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new ValidatePermissionAttribute("Login", "Erro"));
}

我的授权属性:

public class ValidatePermissionAttribute : AuthorizeAttribute
{
    ...

    public ValidatePermissionAttribute(params string[] optionalControllers)
    {
        _optionalControllers = optionalControllers;
    }

    ...
}

更新 3:

条件过滤器是必经之路。

最佳答案

您是否考虑过使用 Conditional Filters in ASP.NET MVC 3

关于c# - 如果我所有的 .Net Controllers/Action 都需要 Authorize Attr,为什么没有一个属性只使用那些不需要的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7474466/

相关文章:

.net - 使用 Github 部署进行 Azure Secret Web.config 转换

c# - 如何隐藏数据表中的列

c# - 如何从后面的代码调用回发?

asp.net - 如何更改 asp.net DropDownList 中文本框区域的背景颜色?

c# - 在模型中绑定(bind)两个表 - ASP.NET MVC Razor

c# - 接口(interface)/抽象类编码标准

c# - 我如何将更多时间添加到 datetime.now 解析它然后重置回原始 datetime.now?

c# - 到期时自动重新填充缓存

c# - 这是工厂方法创建模式吗?

c# - 在 Visual Studio 中运行 .Net 1.1 应用程序时出错