Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Collection.fetch() when after session timeout

I have an ASP.NET MVC4 sub web application added in to an existing ASP.NET WebForm web site. the whole website is using forms authentication.

In my MVC4 client side, I use Backbone.js for building the application, and the client application is most likely a SPA.

Everything works fine, but after session timeout my application does not redirect to log-in page.

I tried the error callback on Collection.fetch method, it was triggered (which is good) when trying to fetch after session time out.

However, the response status code is 200 (OK) with response content is the log-in page content.

So, my question is, in error handler how do I know whether the callback is triggered by session timeout or any other unexpected error?

If determined, how should I do to let Backbone redirect page to log-in page while referring current page?

like image 253
Shuping Avatar asked Aug 24 '26 06:08

Shuping


1 Answers

Here is something what Phil Haack had blogged about

Exceprts from the post :

Possible Solutions

I’m going to cover a few possible solutions I’ve seen around the web and then present the one that I prefer. It’s not that these other solutions are wrong, but they are only correct in some cases.

Remove Forms Authentication

If you don’t need FormsAuth, one simple solution is to remove the forms authentication module as this post suggests. This is a great solution if you’re sole purpose is to use ASP.NET to host a Web API service and you don’t need forms authentication. But it’s not a great solution if your app is both a web application and a web service.

Register an HttpModule to convert Redirects to 401

This blog post suggests registering an HTTP Module that converts any 302 request to a 401. There are two problems with this approach. The first is that it breaks the case where the redirect is legitimate and not the result of FormsAuth. The second is that it requires manual configuration of an HttpModule.

Install-Package MembershipService.Mvc

My colleague, Steve Sanderson, has an even better approach with his MembershipService.Mvc and MembershipService.WebForms NuGet packages. These packages expose ASP.NET Membership as a service that you can call from multiple devices.

Some more info from comment of this blog

We had the same problem. But what we did, was to hook to AuthenticateRequest (just like you did) and we also checked the request to see if it's ajax or not (again, just like what you did). But at this point, we simply returned a JSON like {location: 'http://www.domain.com/path-to-login-page'} and we simply ended response in that method with HTTP code 200. This way, jQuery still gets a JSON result. But if the result has a "location" property, we simply do a client-side redirect to login page. That's our way and it works like a charm.

like image 123
Yasser Shaikh Avatar answered Aug 25 '26 22:08

Yasser Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!