Skip to main content

Microsoft Silverlight

Answered Question Keeping the ASP.NET session time out "sliding" while in Silverlight app?RSS Feed

(0)

anyeone
anyeone

Participant

Participant

811 points

182 Posts

Keeping the ASP.NET session time out "sliding" while in Silverlight app?

Howdy,

I have an application that mixes both Silverlight 2.0 and ASP.NET functionality.  The user logs in through ASP.NET using the Membership API & Forms authentication, with a sliding timeout (currently 30 minutes, but I may have to set it higher), credentials passed down to Silverlight and used for the WCF service calls etc., this all works properly.

99% of what the user does, they do in the Silverlight application.  However occasionally the user will do something in the Silverlight app that touches the ASP.NET functionality, and a new browser window is opened to an ASPX page that does stuff Silverlight doesn't currently support.

The issue is - if the user spends over 30 minutes working in the Silverlight app without accessing the ASPX functionality, and then tries to, their ASP.NET session has timed out.  This is a very common scenario given the workflow of the application.  Forcing the user to log in again is undesirable, not requiring login on these pages is not an option.

So, it seems I have two viable options - raise the session timeout in the web.config (and then force a login again if they exceed it), or what I'd prefer to do is, find some way of communicating back to ASP.NET from Silverlight that the session is still alive - obviously without posting back and resetting the state of the Silverlight application.

Is there a way to do this with AJAX and if so can someone help point me in the right direction?

Thanks in advance

--
Anye Mercy
AnyeDotNet.blogspot.com

Please "Mark as Answer" the posts that help you - this lets others know the problem has been solved and helps others having the same problem know which solution works. Thanks!

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

A simple way to do this would be to create a static class or singleton class that has a DispatcherTimer which would fire on an interval that makes sense (like 5 minutes). Then in ASP.Net create a simple page that does nothing (or for fun you could just return the current session ID). Then use the WebClient to request that page whenever the timer tick event fires. You could use DownloadStringAsync and pass the url to your page that returns the sessions ID and check to make sure you're on the same session.

If you need sample code let me know and I'll try to post some.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

 Hi,

You can use Dispatcher timer object in Silverlight application.

You can check that timer and attach a tick event with that.

In your silverlight application set some flags and in timer's tick event check them .

According to thier status call a javascript function  which will invoke some server side stuff and will raise timeout.

Mark as answer if this post answered your question.

Harsh Bardhan

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

You could create a WCF web service that has a KeepAlive method and just call that from a DispatchTimer. Once you've done that, it would be pretty tempting to add Logon and Logoff methods. :)

anyeone
anyeone

Participant

Participant

811 points

182 Posts

Re: Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

@jack - The WCF service already has logon & logoff methods...they're just called from ASP.NET rather than Silverlight ;)  I can't get away from ASP.NET completely because I'm doing some stuff Silverlight doesn't support/support well such as displaying HTML files, file download etc. and there will eventually be multiple silverlight applications hosted on the site - so it makes sense for me to login using ASP.NET.  If calling the WCF service would keep the session alive, I wouldn't be having this problem - I call the WCF service all the time from Silverlight. But since the WCF service is in another application, ASP.NET doesn't know about it.  I suppose I could look into re-combining the WCF service application and the Silverlight application but honestly I'd rather not do that since the WCF service application is used elsewhere as well. (And with the stupid Arial Unicode MS font package being 22MB, I have enough trouble compiling the Silverlight hosting application already at the size it is...)

 @Bryant - so in your solution, using the WebClient to call the dummy ASP.NET page shows nothing on screen, correct?  It's all behind the scenes in the Silverlight app?  I think this is the direction I need to go.  I'll play around and write back if I get stuck.

--
Anye Mercy
AnyeDotNet.blogspot.com

Please "Mark as Answer" the posts that help you - this lets others know the problem has been solved and helps others having the same problem know which solution works. Thanks!

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Re: Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

anyeone:
@jack - The WCF service already has logon & logoff methods...they're just called from ASP.NET rather than Silverlight

This is a little confusing. You can expose WCF services via ASP.NET, but you call them from either AJAX or Silverlight. It's not clear what you mean when you say that you call them from ASP.NET.

anyeone:
and there will eventually be multiple silverlight applications hosted on the site - so it makes sense for me to login using ASP.NET

This is even more confusing. ASP.NET will allow you to expose/render an HTML page or WCF service. Ultimately it is transparent to ASP.NET whether a particular function was invoked via AJAX or Silverlight.

anyeone:
But since the WCF service is in another application, ASP.NET doesn't know about it.

It sounds like your project is pulling data from another project which exposes a WCF service, and that your project has the membership stuff that you are attempting to keep alive. So what I suggested was to add a WCF service to your ASP.NET app that you could call on a DispatchTimer (which would update the sliding window.)

anyeone:
@Bryant - so in your solution, using the WebClient to call the dummy ASP.NET page

Either downloading a page via webclient or calling a service method will be enough to update the expiration window (without reloading the page on the client.)

anyeone
anyeone

Participant

Participant

811 points

182 Posts

Re: Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

jackbond:

anyeone:
@jack - The WCF service already has logon & logoff methods...they're just called from ASP.NET rather than Silverlight

This is a little confusing. You can expose WCF services via ASP.NET, but you call them from either AJAX or Silverlight. It's not clear what you mean when you say that you call them from ASP.NET.

anyeone:
and there will eventually be multiple silverlight applications hosted on the site - so it makes sense for me to login using ASP.NET

This is even more confusing. ASP.NET will allow you to expose/render an HTML page or WCF service. Ultimately it is transparent to ASP.NET whether a particular function was invoked via AJAX or Silverlight.

anyeone:
But since the WCF service is in another application, ASP.NET doesn't know about it.

It sounds like your project is pulling data from another project which exposes a WCF service, and that your project has the membership stuff that you are attempting to keep alive. So what I suggested was to add a WCF service to your ASP.NET app that you could call on a DispatchTimer (which would update the sliding window.)

anyeone:
@Bryant - so in your solution, using the WebClient to call the dummy ASP.NET page

Either downloading a page via webclient or calling a service method will be enough to update the expiration window (without reloading the page on the client.)

 

The front page of the application is a pure ASP.NET page with a login form.  When the user clicks "Login" my custom membership provider calls the WCF service to verify credentials, if successful, then the asp.net page sets the authentication cookie.  So I am not calling them from Silverlight or AJAX - I haven't even GOTTEN to the page with my Silverlight app yet.  No page in the application is accessible w/o a login thanks to the Membership API & my web.config.

Think of it this way.  User logs in once in the ASP.NET application (that happens to host one or more Silverlight apps, each on its own ASP.NET page) and is directed to a landing page (also ASP.NET) where they pick which Silverlight app they need, they are then redirected to the ASP.NET page hosting the Silverlight app they want to use.  At least one of these Silverlight apps may need to launch a separate window with another pure ASP.NET page from the application on it, and functionality on that page requires the session still be intact.

I think your confusion stems from not understanding that I have actual ASP.NET pages before I even get to a Silverlight application.

Yes, as I mentioned the WCF services I am using are in a separate application, which is not the one having the session I'm trying to keep alive.  The session is on the application that hosts the Silverlight apps.  Since I don't already have a WCF service exposed in the core application it sounds simpler to use a dummy page than add a service but it sounds like either would work.

 

--
Anye Mercy
AnyeDotNet.blogspot.com

Please "Mark as Answer" the posts that help you - this lets others know the problem has been solved and helps others having the same problem know which solution works. Thanks!

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Re: Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

anyeone:
The front page of the application is a pure ASP.NET page with a login form.  When the user clicks "Login" my custom membership provider calls the WCF service to verify credentials, if successful, then the asp.net page sets the authentication cookie.  So I am not calling them from Silverlight or AJAX - I haven't even GOTTEN to the page with my Silverlight app yet.  No page in the application is accessible w/o a login thanks to the Membership API & my web.config.

I had a very similiar application, i.e. ASP.NET page with login form. User clicks login, code behind for that page validates user and sets authentication cookie. So my question is, other than the time required to implement it, why can't you move the exact logic you have in your aspx.cs code behind into your own WCF service? Then your login page could be a Silverlight app which calls the service, and voila, your ASP.NET session is set. Now that they've logged in, the service call back redirects to a standard ASP.NET landing page, or it could dynamically load a Silverlight landing page. With a little refactoring, if you implement a Silverlight landing page, when the user selects an app, instead of redirecting to another page, you could simply dynamically load that app. One benefit of this, is you could put the KeepAlive logic in the Silverlight "master" page. Obviously this is beyond the scope of your original question, but as you're usage of Silverlight grows, you'll probably want better integration between your apps. One last note, as the ASP.NET session has been set, any one of the Silverlight apps can open a plain jane ASP.NET html page at any time.

Perhaps the confusion is that it seems you are under the impression that the ASP.NET session cookie must be set in a login page code behind?

anyeone
anyeone

Participant

Participant

811 points

182 Posts

Re: Re: Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

 Because I still have the issue that there is some functionality that isn't supported or supported well in Silverlight. And, I'm not showing Plain Jane html pages, I'm dynamically streaming the content from a server, which requires an ASPX page to call the server, verify the user is allowed to see the doc they're asking for and then set the response appropriately.  There is not a good method of rendering HTML inside Silverlight yet although some people have built rich text controls, that's just overkill for what I'm doing and frankly the ones I tested weren't that great.

Why can't you believe me when I say I have reasons that some of this functionality needs to stay in ASP.NET for awhile? It's not just the login.  I'm not a complete n00b ;)

--
Anye Mercy
AnyeDotNet.blogspot.com

Please "Mark as Answer" the posts that help you - this lets others know the problem has been solved and helps others having the same problem know which solution works. Thanks!

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Re: Re: Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

Yikes, I'm not saying re-write every page. What I am saying is that you can login and keep alive ASP.NET session via web services. You yourself have said that 99% of what the user does is in Silverlight, so why not the login? From my experience, it greatly simplified development AND improved user experience by not jumping from one Silverlight html page to another Silverlight html page. I understand that you have pages that can't be done in Silverlight and that those pages rely on the ASP.NET user session cookie, but there's no reason that cookie can't be set via a Silverlight app, and it's that part that you don't seem to be grasping.

anyeone:
I'm not a complete n00b

Never said that.

polarlinks
polarlinks

Member

Member

14 points

4 Posts

Answered Question

Re: Re: Re: Keeping the ASP.NET session time out "sliding" while in Silverlight app?

Anyeone, create a wcf service on the web app. Use the timer to call it at an interval. On the service side add "system.web" and make sure you assign something to the current session. Session["key"] = "doesntMatter";

I tested this and it works =).

GetSessionInfo.svc

 

 [OperationContract]
        public string GetSessionID()
        {
            string ret = string.Empty;
            if (HttpContext.Current.Session.IsNewSession)
                ret += "New Session!";
            if (HttpContext.Current.Session["key"] == null)
            {
                ret += " null value!";
                HttpContext.Current.Session["key"] = "key";
            }
            ret +=  HttpContext.Current.Session.SessionID;
            return ret;
        }
 

 

If you click a button from silverlight that calls that and gets the data back you will keep it from timing out. So stick that in a timer. If you stop calling it and idle you will timeout the app. I'm building a mixed mode app also. I'll leave it up to you to figure out how to disable the timer when the user has actually quit using the silverlight app. I'm noobing up silverlight =\.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities