1

HttpApplication.AuthenticateRequest请问能解释一下和HttpApplication.AuthorizeRequestin的区别ASP.NET MVC 3吗?它们什么时候会发生?假设这种情况:

User有一个名为的属性IsBanned,我想IsBanned在每个请求中检查他/她的属性。如果是true,我将其重定向User到错误页面。但不是针对所有请求,只是请求他们的动作由[Authorize]属性签名。好的,在这种类型的动作中,会HttpApplication.AuthenticateRequest发生还是HttpApplication.AuthorizeRequest什么?

我知道我可以检查这个属性SignIn|LogOn。但我的意思是:

  • 用户请求登录
  • 我检查了财产IsBanned,它是false
  • 用户登录
  • 用户查看网站的某些页面
  • 管理员禁止用户(当他登录时)
  • 用户请求具有[Authorize]属性的页面(操作)
  • 用户已登录(在此之前。还记得吗?)
  • 所以我必须显示请求的页面
  • 但是用户给了管理员禁止的标志
  • 如何阻止用户查看请求的页面?

提前致谢。

4

1 回答 1

1

我认为您不需要处理HttpApplication.AuthenticateRequestor中的任何一个HttpApplication.AuthorizeRequest。我会通过使用自定义Authorize属性来解决它。

public class MyAuthorizeAttribute : AuthorizeAttribute {

        protected override bool AuthorizeCore(HttpContextBase httpContext) {
            bool authorizerPrimarily = base.AuthorizeCore(httpContext);

            if(authorizedPrimarily){
               return user_not_banned;
            }

            return authorizerPrimarily;
        }
}

您可以从中获取用户名httpContext.User.Identity.Name。使用它从数据库中获取数据。

更新评论 1

要将被禁止的用户重定向到特定页面,您可以这样做:

if(authorizedPrimarily){
   if(user_banned){
      httpContext.Response.Redirect("url of banned message");
      return false;
   }

  return true;
}
于 2012-07-11T11:55:18.293 回答