覆盖ASP.NET MVC中的授权属性

时间:2009-04-14 10:21:06

标签: .net asp.net-mvc security authentication authorize

我有一个MVC控制器基类,我在其上应用了Authorize属性,因为我希望几乎所有控制器(及其操作)都被授权。

但是我需要一个控制器和另一个控制器的动作未经授权。我希望能够使用[Authorize(false)]或其他东西来装饰它们,但这不可用。

有什么想法吗?

4 个答案:

答案 0 :(得分:98)

编辑:自ASP.NET MVC 4以来,最好的方法就是使用内置的AllowAnonymous属性。

下面的答案是指早期版本的ASP.NET MVC

您可以使用可选的bool参数创建继承自标准AuthorizeAttribute的自定义授权属性,以指定是否需要授权。

public class OptionalAuthorizeAttribute : AuthorizeAttribute
{
    private readonly bool _authorize;

    public OptionalAuthorizeAttribute()
    {
        _authorize = true;
    }

    public OptionalAuthorizeAttribute(bool authorize)
    {
        _authorize = authorize;
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if(!_authorize)
            return true;

                    return base.AuthorizeCore(httpContext);
    }
}

然后您可以使用该属性修饰基本控制器:

[OptionalAuthorize]
public class ControllerBase : Controller
{
}

对于您不希望授权的任何控制器,只需使用带有'false'的覆盖 - 例如

[OptionalAuthorize(false)]
public class TestController : ControllerBase
{
    public ActionResult Index()
    {
        return View();
    }
}

答案 1 :(得分:75)

似乎ASP.NET MVC 4通过添加AllowAnonymous属性来“修复”这个问题。

David Hayden wrote about this

[Authorize]
public class AccountController : Controller
{
    [AllowAnonymous]
    public ActionResult Login()
    {
        // ...
    }

    // ...
}

答案 2 :(得分:15)

我个人对此的看法是拆分控制器。只需创建另一个控制器对于您不需要身份验证的操作。

或者你可以:

  • BaseController
    不需要身份验证 - 这里有你所有的“基础资料”:)。

  • BaseAuthController : BaseController
    此处的所有操作都需要身份验证。

通过这种方式,您可以根据需要进行身份验证,只需从特定类派生即可。

答案 3 :(得分:6)

如果您只想在其他授权的控制器上执行某项操作,您可以执行以下操作:

public class RequiresAuthorizationAttribute : ActionFilterAttribute
{
    private readonly bool _authorize;

    public RequiresAuthorizationAttribute()
    {
        _authorize = true;
    }

    public RequiresAuthorizationAttribute(bool authorize)
    {
        _authorize = authorize;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var overridingAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof (RequiresAuthorizationAttribute), false);

        if (overridingAttributes.Length > 0 && overridingAttributes[0] as RequiresAuthorizationAttribute != null && !((RequiresAuthorizationAttribute)overridingAttributes[0])._authorize)
            return;

        if (_authorize)
        {
            //redirect if not authenticated
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                //use the current url for the redirect
                var redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;

                //send them off to the login page
                //var redirectUrl = string.Format("?RedirectUrl={0}", redirectOnSuccess);
                var loginUrl = LinkBuilder.BuildUrlFromExpression<HomeController>(filterContext.RequestContext, RouteTable.Routes,
                                                                                  x => x.Login(redirectOnSuccess));
                filterContext.HttpContext.Response.Redirect(loginUrl, true);
            }
        }
    }
}
相关问题