通过扩展FormsAuthentication票证来阻止AJAX Timer Control请求?

时间:2011-07-05 22:54:23

标签: asp.net ajax asp.net-ajax timer forms-authentication

我有一个使用一些ASP.NET AJAX Timer控件(即轮询)的webforms应用程序。如果用户位于包含其中一个用户的页面上,则他们实际上永远不会超时,因为轮询过程会使其身份验证票证保持活动状态。

我想分割Timer控件,这样它们就不会触发Forms Authentication的RenewTicketIfOld方法。我正在进行的路径和以前做过类似的事情是在AJAX HTTP请求中注入一些内容,将这些请求标识为来自计时器,然后在隐藏身份验证cookie的表单身份验证模块之后运行一些代码在回复中被送回。

有关如何防止Timer控件使表单身份验证票证保持活动的任何其他建议吗?

2 个答案:

答案 0 :(得分:1)

来到我的第一个技巧。

在网络配置上,设置域名www.yoursite.com

<forms domain="www.yoursite.com" .... >

并创建一个类似timers.yoursite.com的子域,实际上与www.yoursite.com相同。 现在在times.yoursite.com 上进行调用,并且因为Cookie必须找到www.yoursite.com,所以它们永远不会触发身份验证。

第二个肮脏的把戏

在web config上设置requireSSL = true

<forms requireSSL="true" 

并在非安全页面上进行计时器调用。这样,身份验证不会再次触发,因为现在不会在非安全页面上读取cookie。

最后的想法,在cookie少页面和无会话页面上运行时间,我的意思是一个页面根本不发送或获取cookie。我不知道在相同的域名页面下是否可行,我认为你需要一个不同的域名(出局)

答案 1 :(得分:1)

取得进展,目前这是我的解决方案。我从在Timer AJAX请求中设置自定义标头并检查模块中的标头(您可以在答案版本历史中看到这一点)到一个简单的仅模块解决方案。 (帽子提示How to tell if a refresh came from a Timer问题)

public class SkipAuthTicketRenewalModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += new EventHandler(context_EndRequest);
    }

    void context_EndRequest(object sender, EventArgs e)
    {
        // See if auth cookie was added in response to the timer control update by the FormsAuthModule, 
        // indicating the ticket was renewed.  If it was, remove it so we don't extend the ticket.

        HttpContext ctx = HttpContext.Current;
        string ctrlname = ctx.Request.Params.Get("__EVENTTARGET");

        if (!String.IsNullOrEmpty(ctrlname))
        {
            Page page = ctx.Handler as Page;
            if (page != null)
            {
                Control ctrl = page.FindControl(ctrlname);
                if (ctrl != null && ctrl is Timer)
                {
                    ctx.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                }
            }
        }
    }
}
相关问题