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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1066090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:23:50  来源:igfitidea点击:

Preventing an ASP.NET Session Timeout when using Silverlight

c#asp.netsilverlightauthenticationsession

提问by Russell Patterson

I'm writing a program which has both an ASP.NET configuration system and a Silverlight application. Most users will remain on the Silverlight page and not visit the ASP.NET site except for logging in, etc.

我正在编写一个程序,它同时具有 ASP.NET 配置系统和 Silverlight 应用程序。大多数用户将留在 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?

有任何想法吗?

采纳答案by NerdFury

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. Be sure to have your Handler class implement IRequiresSessionState.

在托管 Silverlight 控件的页面上,您可以设置一个 javascript 计时器并每 5 分钟对 Http 处理程序 (.ashx) 进行一次ajax 调用,以保持会话处于活动状态。一定要让你的 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

更新

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. Fiddler is a great tool for debugging ajax calls, if you don't use it, start.

间隔以毫秒为单位,所以我的样本将每 5 秒 ping 一次,您可能希望它是一个更大的数字。Fiddler是一个很好的调试ajax调用的工具,如果你不使用它,就开始吧。

回答by Russell Patterson

I've actually found a pretty cool hack which essentially embeds an iframe on the same page as the silverlight application. The iframe contains an aspx webpage which refreshes itself every (Session.Timeout - 1) minutes. This keeps the session alive for however long the silverlight app is open.

我实际上发现了一个非常酷的 hack,它基本上将 iframe 嵌入到与 Silverlight 应用程序相同的页面上。iframe 包含一个 aspx 网页,它每 (Session.Timeout - 1) 分钟刷新一次。无论 Silverlight 应用程序打开多久,这都会使会话保持活动状态。

To do this:

去做这个:

Create an asp.net page called "KeepAlive.aspx". In the head section of that page, add this:

创建一个名为“KeepAlive.aspx”的asp.net 页面。在该页面的头部部分,添加以下内容:

<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 会话将保持活动状态!

回答by Kerry Todyruik

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”,这效果更好。