.net mvc & ajax forms - how to handle session expired -
To check the expired session in my Net Networks application, I have a client filter attribute on the standard HTML form works fine. However, when I am called by an AJAX form and enforces an expiry end of the session, then it reloads the login page (where the filter attribute redirects the reference) in the Ajax update container. Does anyone know how to redirect the whole page to the login page if the session has expired, when the AJAX call is done or does anyone know how to handle this situation a little more beautifully? . Thanks in advance
Public category SessionExpiredFilterAttribute: ActionFilterAttribute {Public override zero OnActionExecuting (ActionExecutingContext filterContext) {HttpContext ctx = HttpContext.Current; // if checked (ctx.Session! = Null) {session is supported / checks, then a new session ID was generated (ctx.Session.IsNewSession) {// If it says that it is There is a new session, but if an existing cookie is present, then it should be // string session has expired cookie = ctx.Request.Headers ["cookie"]; If ((zero = sessionCookie) & amp;! (SessionCookie.IndexOf ("ASP.NET_SessionId")> = 0)) {//SessionHelper.AddPageMessage(_message "," Your session has expired Please log in to continue. "); Ctx.Response.Redirect ("~ / account / logon"); }}} Base.OnActionExecuting (filterContext); }}
I posted the original question for this and I think that in the end I One solution for this though it was not as elegant as I had hoped, it works (which I have found so far). The solution is around onbusiness awakening and some jazzeries. My ActionLink (this may also be an Ajax.BeginForm that applies to this) looks like this
<% = Ajax.ActionLink ("AjaxAction", the new " Do azax function "{}, new AjaxOptions {OnBegin =" check_session ", onSuccess =" ajaxSuccess ", UpdateTargetId =" update_container "}, new {})%>
There is a controller action in my account administrator who checks the session, in my case I am storing user objects so I check to see if this is empty , And 'True' or 'OutputCache' as false content otherwise your results will be cached. [OutputCache (Duration = 0, VaryByParam = "None")] Public ActionResult CheckAjaxSessionExpired () {bool _sessionExpired = false. If (session ["user"] == tap) {_sessionExpired = true; } Return contents (_sessionExpired.ToString ()); }
The last part is the check_session
function is specified as an on-line handler. The jazzyjjj hits the probe action of the call session and checks the reaction. If the response is 'true' (which means the session has expired), then it sets the page's location back to the home page (or Google in this case).
function check_session () {Var url = '& lt;% = Url.Action ("CheckAjaxSessionExpired", "account")%>'; // Check session $ .ense ({url: url, success: function (data) {// alert (data); if (data == "true") {document.location = "http://www.google. Com ";}}, Error: Action () {Warning (" There was a problem loading the test of AJAX session ")}}}); }
There are several things to remember when using this method, the first is that it will run in asynchronous form. If you have a data sensitive code that you are calling, then you will need to check server side on session. This code is only for redirect to a login page (or home page), if the session has expired during an AJAX call. The other thing is that the check_session
function will need to wired all your Ajax calls in the OnBegin
event. If anyone has any suggestions or suggestions for improving this method, then I would love to hear them. I hope it helps someone.
Comments
Post a Comment