How to check session is expired or not and redirec

2019-07-25 08:28发布

问题:

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
  }