How to check session is expired or not and redirect user to login page using javascript

夙愿已清 提交于 2019-12-24 10:34:57

问题


I am developing web application using ASP.Net MVC. In my application I am using some many ajax calls through out my application to Get/Post data for some functionality. For some functionality I am showing iframed popup. My issue is that when session is expired in my application I am redirecting user to login page but if user try to access functionality that opens in iframed popup instead of redirecting to login page my popup opens with login page inside it otherwise it's working properly. So what is best way to check session is expired or not and redirecting user to login page from inside of iframed popup. The code for opening iframed popup is as follow:

function ShowPopup(Url, DialogTitle, W, H) {

/* Setting for dialog box */
$("#dialog").dialog({
    title: DialogTitle,
    autoOpen: false,
    modal: true,
    resizable: false,
    width: W,
    height: H,
    open: function (ev, ui) {
        /* Setting URL to open in iframe */
        $('#ContentIframe').attr('src', Url);
    }
});

/* Opening dialog box */
$('#dialog').dialog('open');
}

回答1:


It can be done using following logic...

Write following block of code in your Login Page

 <script type="text/javascript">
window.onload=function()
{
    if (window!=window.top) 
    { 
        window.top.location = "YourLoginPageURL";
        window.close();

    }
};
</script>

(window!=window.top) will return true if Login page is opened in IFra

Hope it helped




回答2:


Make as to check session

function CheckUserSession(svcUrl) {
var userInSession = false;
$.ajax({
    url: Your Url,// It will return true/false based on session
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    async: false,
    success: function (result) {
        userInSession = result.userInSession;
    }
  });
  return userInSession;
}

Before calling your code check this

 if(CheckUserSession() == false){
            ShowLogin();
 }
else{

     //Your code
  }


来源:https://stackoverflow.com/questions/24631462/how-to-check-session-is-expired-or-not-and-redirect-user-to-login-page-using-jav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!