views:

2297

answers:

4

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.

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.

Any ideas?

+1  A: 

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.

To do this:

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

<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:

<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!

rwponu
Unfortunately, it looks like this doesn't work in IE8(at least).
rwponu
+5  A: 

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.

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.

You will also need to set the response cache properly to make sure that the browser makes the ajax call each time.

UPDATE

Here is the sample code for an 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

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.

NerdFury
It is more lightweight than a nearly-blank aspx page?I'm kind of a newbie to ASP.NET/AJAX and this seems like it's more complicated. Can you provide a sample of the ashx file I would need to use?
rwponu
I've tried this, and the javascript doesn't seem to work in IE8. I just downloaded the latest version of jQuery for this purpose.Is there something else that the handler/page needs?
rwponu
There isn't anything else you should need. There is a great tool called Fiddler2 you can use to watch network traffic and make sure that the call is being made. It could be a path problem, in which case fiddler would report a 404. I would recommend using that tool to see what is happening. Otherwise, IE8 has good javascript debugging tools that might also get you closer.
NerdFury
@NerdFury is it necessary to use handler if I'm using asp.net-mvc ?
Omu
if you are calling a handler, yes, you still need it. If you are making a call to a controller action, then you would not need it.
NerdFury
A: 

The ajax ping / HttpHandler approach is good, but the JQuery $.get function is expecting a json result and throws a javascript parse error.

I modified the Ping HttpHandler to return "{}" instead of "OK" and this worked better.

Kerry Todyruik
you can specify the type of response you want to get when using $.get
Omu
A: 

What if you still want your Silverlight Session to expire? You want to keep it alive only when the user is active, right?

Menard Soliven