简体   繁体   English

使用Silverlight时防止ASP.NET会话超时

[英]Preventing an ASP.NET Session Timeout when using Silverlight

I'm writing a program which has both an ASP.NET configuration system and a Silverlight application. 我正在编写一个既包含ASP.NET配置系统又包含Silverlight应用程序的程序。 Most users will remain on the Silverlight page and not visit the ASP.NET site except for logging in, etc. 大多数用户将保留在Silverlight页面上,除了登录等外,不会访问ASP.NET站点。

The problem is, I need the session to remain active for authentication purposes, but the session will timeout even if the user is using the features of the silverlight app. 问题是,我需要会话保持活动以进行身份​​验证,但即使用户使用silverlight应用程序的功能,会话也会超时。

Any ideas? 有任何想法吗?

On the page hosting the silverlight control, you could setup a javascript timer and do an ajax call to an Http Handler (.ashx) every 5 minutes to keep the session alive. 在托管silverlight控件的页面上,您可以设置一个javascript计时器并每隔5分钟对一个Http Handler(.ashx)执行ajax调用以保持会话处于活动状态。 Be sure to have your Handler class implement IRequiresSessionState . 确保您的Handler类实现IRequiresSessionState

I recommend the Handler because it is easier to control the response text that is returned, and it is more lightweight then an aspx page. 我推荐使用Handler,因为它更容易控制返回的响应文本,并且它比aspx页面更轻量级。

You will also need to set the response cache properly to make sure that the browser makes the ajax call each time. 您还需要正确设置响应缓存,以确保浏览器每次都进行ajax调用。

UPDATE UPDATE

Here is the sample code for an HttpHandler 以下是HttpHandler的示例代码

public class Ping : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.ContentType = "text/plain";
        context.Response.Write("OK");
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

Then if you use jQuery, you can put this on your host aspx page 然后,如果你使用jQuery,你可以将它放在你的主机aspx页面上

setInterval(ping, 5000);

function ping() {
    $.get('/Ping.ashx');
}

The interval is in milliseconds, so my sample will ping every 5 seconds, you probably want that to be a larger number. 间隔以毫秒为单位,因此我的示例将每5秒ping一次,您可能希望它是一个更大的数字。 Fiddler is a great tool for debugging ajax calls, if you don't use it, start. Fiddler是调试ajax调用的好工具,如果不使用它,请启动。

I've actually found a pretty cool hack which essentially embeds an iframe on the same page as the silverlight application. 我实际上发现了一个非常酷的黑客,它基本上将一个iframe嵌入到与silverlight应用程序相同的页面上。 The iframe contains an aspx webpage which refreshes itself every (Session.Timeout - 1) minutes. iframe包含一个aspx网页,每隔(Session.Timeout - 1)分钟刷新一次。 This keeps the session alive for however long the silverlight app is open. 无论Silverlight应用程序打开多长时间,都会使会话保持活动状态。

To do this: 去做这个:

Create an asp.net page called "KeepAlive.aspx". 创建一个名为“KeepAlive.aspx”的asp.net页面。 In the head section of that page, add this: 在该页面的head部分中,添加以下内容:

<meta id="MetaRefresh" http-equiv="refresh" content="18000;url=KeepAlive.aspx" runat="server" />

    <script language="javascript" type="text/javascript">
        window.status = "<%= WindowStatusText%>";
    </script>

In the code behind file, add this: 在代码隐藏文件中,添加以下内容:

protected string WindowStatusText = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            // Refresh this page 60 seconds before session timeout, effectively resetting the session timeout counter.
            MetaRefresh.Attributes["content"] = Convert.ToString((Session.Timeout * 60) - 60) + ";url=KeepAlive.aspx?q=" + DateTime.Now.Ticks;

            WindowStatusText = "Last refresh " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
        }
    }

Now, on the same page as the silverlight app, add this: 现在,在与silverlight应用程序相同的页面上,添加以下内容:

<iframe id="KeepAliveFrame" src="KeepAlive.aspx" frameborder="0" width="0" height="0" runat="server" />

Now the asp.net session will remain active while the silverlight app is being used! 现在,当使用silverlight应用程序时,asp.net会话将保持活动状态!

The ajax ping / HttpHandler approach is good, but the JQuery $.get function is expecting a json result and throws a javascript parse error. ajax ping / HttpHandler方法很好,但是JQuery $ .get函数期望json结果并抛出javascript解析错误。

I modified the Ping HttpHandler to return "{}" instead of "OK" and this worked better. 我修改了Ping HttpHandler以返回“{}”而不是“OK”,这样做效果更好。

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

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