AJAX timeout issue in MVC - asp.net-mvc

I have an ASP.Net MVC application. I am using an AJAX request on a page which requires user authentication to fire an action on another controller, which returns a view to update a table on the page. The action that the AJAX request makes also requires authentication. The issue comes up when the user lets their session timeout and then does something to fire the AJAX request. The entire page does not redirect to the login page. Instead, the view returned to the AJAX request is the login page, which then is populated inside the div meant for the refreshed data table.
Is there a way to at least have the request return an error message instead of the login view?

Sure, you could create your own Authorize attribute and check if HttpContext.Current.Request.Headers["XMLHttpRequest"] then return error message else redirect to login page

Related

Redirect from plugin in ajax request in ZF2

I have a application in ZF2 framework, in this I am sending and requesting data to/from a web services using http_client request. In my application I have a multi step form which is changing form steps using ajax.
Now if I submit a form using ajax request, it hit my controller and then from controller I call a common function of controller plugin for all type of request in this case if there is any error in web service then I want to redirect to login page but it not redirecting parent layout/page instead of this it show the login page in ajax loaded form part(where my form is changing through ajax).
Please help me to redirect main page to login page instead of show login page in ajax loaded part.
Thanks in advance
I'm not a javascript expert but you need to do the redirect in the javascript and not your controller.
window.location.replace("http://stackoverflow.com");
You can pass the redirect url from your controller if you need to redirect to different locations depending on your logic.
See this question.
Hope this points you in the right direction.

Spring security authentication - looks like infinity loop but it works. How?

how does it really works?
I wrote a sample app using spring boot with security and thymeleaf.
As [tutorial]: http://www.thymeleaf.org/doc/articles/springsecurity.html shows ones just need to create controller and login page. But...
In security config there is fragment as
loginPage("/login.html")
then in controller there is request mapping for /login like
#RequestMapping("/login.html")
public String login() {
return "login.html";
}
and then in thymeleaf page there is action mapping like
<form th:action="#{/login.html}" method="post">
So..when ones try to access restricted page is redirected to login page which means that login.html is displayed. Then after filling the form user clicks the button and trigger action which is mapped on controller that returns login.html once again. Looks like a loop. But it works. How??
I think I see the confusion here. In recent versions of Spring Security, it is possible and even encouraged by default that the login page and login processing URL are the same address (but they don't need to be). This is possible because of different HTTP methods.
When redirected to login page, the page is retrieved using a GET request. The authentication filter sees this, but passes on the request to Spring MVC and the controller, since it is a GET request.
When submitting the form, the username and password are sent in a POST request. The authentication filter intercepts this request since it is POST, performs the authentication and takes action depending on the result. The request is not forwarded to the controller in this case.

Session time out from sub web application

I have a legacy asp.net web form web site, it uses forms authentication. Now, I have the requirement to create a new sub MVC application in the web site. My new application is most likely a SPA that means most actions are done through Ajax. Everything works fine until session time out, because once time out, my background Ajax returns the content of form login page instead of my json data. This is correct session behavior, but how can I know session time out in ajax and then redirect the page to login page?
You can trap the failure of your AJAX calls and look for 401 result, which means that the request was not authorised.
I'm assuming you're using Web API for your service calls here - if you're calling methods on your normal MVC controller you'll need to do a little bit of work to ensure unauthenticated requests return 401 instead of redirecting you to the login page.

How to catch a session timeout when using GET to insert html?

I have a JRuby on Rails application using a lot of Ajax GET calls. The result is inserted in the HTML page. If the session times out, the resulting html is the login page. This login page is then inserted into the current HTML page. Instead, a redirect to the login page should happen...
How is this problem generally tackled?
You can't catch the 302 because browsers hide that from you - your ajax request just sees the final request.
When this has been an issue I usually change my login code such that if access is attempted to a restricted page by a non logged in user, and the request is an ajax request I render a special status code (usually a 4xx code). Check for that status code in your ajax request handlers (depending on jour ajax library, you can usually set up a global handler) and if you see it, redirect the user to the login page.

Make ajax get redirect main page to login when auth times out

I'm using ASP.Net MVC beta 1 and I'm using the asp.net membership provider with the standard authentication controller to restrict access to my site.
I'm using ajax functionality to provide e.g. editing of values by loading partial views into a div with either jQuery $.get/$.ajax or with the Ajax.Actionlink MVC helper. This all works fine most of the time.
My problem comes once the login times out and you click on one of the ajax edit links - the ajax call returns the login page which is put into the div normally used for the edit form.
I want to find a way to redirect the whole page to the login form, when the authentication has timed out and an ajax link is clicked.
One way I can think of is looking at the html returned from the ajax call in the response callback and searching for the 'login' text or form field and doing a redirect from there - but this doesn't feel very clean - is there a better way?
This might help some:
Bypass Forms Authentication auto redirect to login, How to?
From the above answer it looks like http 403 isn't intercepted by Forms Authentication, so you can roll your own ActionFilter that returns an http 403 response if its an Ajax Request and Authorization failed.
On the client side, you could then check the response code for 403, and redirect to the appropriate login url.
There are probably other ways to do this as well!

Resources