简体   繁体   English

ActionFilterAttribute - 适用于特定控制器类型的操作

[英]ActionFilterAttribute - apply to actions of a specific controller type

I'm using an ActionFilterAttribute to do custom authentication logic. 我正在使用ActionFilterAttribute来执行自定义身份验证逻辑。 The Attribute will only be used on a derived Controller class that contains my authentication logic. 该属性仅用于包含我的身份验证逻辑的派生Controller类。

Here's my Controller, derived from my custom controller class, and a sample attribute: 这是我的控制器,源自我的自定义控制器类,以及一个示例属性:

public class MyController : CustomControllerBase
{

   [CustomAuthorize(UserType = UserTypes.Admin)]
   public ActionResult DoSomethingSecure()
   {
      return View();
   }

}

Here's an example of my ActionFilterAttribute: 这是我的ActionFilterAttribute的一个例子:

public class CustomAuthorizeAttribute : ActionFilterAttribute
{
   public MyUserTypes UserType { get; set; }

   public override void OnActionExecuting(ActionExecutingContext filterContext)
   {
      myUser user = ((CustomControllerBase)filterContext.Controller).User;

      if(!user.isAuthenticated)
      {
         filterContext.RequestContext.HttpContext.Response.StatusCode = 401;
      }
   }
}

Works great. 效果很好。

Here's the question: Can I demand that this attribute ONLY be used on Actions in my custom controller type? 这是一个问题:是否可以要求此属性仅用于我的自定义控制器类型中的操作?

You can put the ActionFilter on the class itself. 您可以将ActionFilter放在类本身上。 All actions in the class will realize the ActionFilter. 该类中的所有操作都将实现ActionFilter。

[CustomAuthorize]
public class AuthorizedControllerBase : CustomControllerBase
{
}

public class OpenAccessControllerBase : CustomControllerBase
{
}

public class MyRealController : AuthorizedControllerBase 
{
    // GET: /myrealcontroller/index
    public ActionResult Index()
    {
        return View();
    }
}

Based on the comments and the constraints of my system, I took a hybrid approach. 根据我的系统的评论和限制,我采取了混合方法。 Basically, if the request comes through via a cached route or the "User" is not set for any reason, authentication fails in the proper way. 基本上,如果请求是通过缓存路由发出的,或者由于任何原因未设置“用户”,则身份验证会以正确的方式失败。

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
  private MyUser User { get; set; }

  public override void OnAuthorization(AuthorizationContext filterContext)
  {
    //Lazy loads the user in the controller.
    User = ((MyControllerBase)filterContext.Controller).User;

    base.OnAuthorization(filterContext);
  }

  protected override bool AuthorizeCore(HttpContextBase httpContext)
  {
    bool isAuthorized = false;
    string retLink = httpContext.Request.Url.AbsolutePath;

    if(User != null)
    {
      isAuthorized = User.IsValidated;
    }

    if (!isAuthorized)
    {
      //If the current request is coming in via an AJAX call,
      //simply return a basic 401 status code, otherwise, 
      //redirect to the login page.
      if (httpContext.Request.IsAjaxRequest())
      {
        httpContext.Response.StatusCode = 401;
      }
      else
      {
        httpContext.Response.Redirect("/login?retlink=" + retLink);
      }
    }

    return isAuthorized;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM