我如何知道当前的Action在MVC上的ActionFilterAttribute中是否通过了授权?

时间:2014-10-31 15:27:28

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

如您所知,如果我们想要授权我们可以重写 AuthorizeAttribute ,但目前我想知道当前的操作是否在 ActionFilterAttribute

中通过了授权

例如:这是授权:

public class AdminAuthorizeAttribute : AuthorizeAttribute
{
     public override void OnAuthorization(AuthorizationContext filterContext)
     {
           // if authorize then continue, or else redirection the action to some page for some user
     }
}

这是用于访问日志,这是我想知道的地方当前请求是否被授权

public class LoggerFilterAttribute : ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext filterContext)
   {
     // before save the access log, I want to know current request is authorized or not, then I can write the authorize status to the access log/
   }
}

UPDATE1:

不使用默认的User.Identity或Role。 我在数据库和会话中使用自己的authrize角色。 例如:

public class AdminAuthorizeAttribute : AuthorizeAttribute
    {
        public string ActionName {get; set;}
         public override void OnAuthorization(AuthorizationContext filterContext)
         {
              if (null == GetSession("AdminNo"))
                {
                    // Authorized failed. redirect
                }

              if (null == GetSession("RoleName")
                {
                   // Authorized failed. redirect
                }
              else{
                     if (!GetSession("ActionInRole").include(ActionName))
                     {
                        // Authorized failed. redirect
                     }
                  }

           // Authorized success.
         }
    }

0 个答案:

没有答案