在.Net中验证Cookie过期

时间:2010-01-12 16:52:32

标签: asp.net asp.net-ajax cookies extjs rest

我正在使用ASP.Net的表单身份验证开发应用程序。客户端还对服务器进行RESTfull调用(前端的ExtJS组件)。

我们正在使用自定义HttpHandler进行服务调用。

我的问题是,无论何时认证cookie到期,我都不会调用HttpHandler的ProcessRequest方法,以便我检查cookie是否缺席并重定向用户再次登录。

一个例子是用户离开页面然后在20分钟内返回并点击异步加载的下拉列表。该应用程序只是挂起,永远不会到达我的处理程序。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

强烈建议阅读MSDN杂志文章Securely Implement Request Processing, Filtering, and Content Redirection with HTTP Pipelines in ASP.NET中标题为“管道事件模型”的部分。

简而言之,在将请求移交给HttpHandler中的ProcessRequest()之前,很快就会执行身份验证。如果需要处理这些情况,则需要挂钩管道事件(例如BeginRequest或Authenticate Request)并添加自己的处理程序,如下所示:

public class EnableWebServicesModule : 
               IHttpModule
  {

    public void Init(HttpApplication app)
    {
      // register event handler
      app.BeginRequest +=  new EventHandler(this.OnBeginRequest);
    }

    public void OnBeginRequest(object obj, EventArgs ea)
    {
      // Check if security works here by looking for the cookie or
      // the user context.

    }

    ...
}

有关这个引人入胜且令人兴奋的话题的进一步阅读,check Rich Strahl's walkthrough: A low-level Look at the ASP.NET Architecture