jQuery 如何检查会话是否过期并使用javascript将用户重定向到登录页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24631462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:36:56  来源:igfitidea点击:

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

jqueryasp.net-mvcsessioniframe

提问by CodeWarrior

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:

我正在使用 ASP.Net MVC 开发 Web 应用程序。在我的应用程序中,我在我的应用程序中使用了许多 ajax 调用来获取/发布某些功能的数据。对于某些功能,我正在显示 iframe 弹出窗口。我的问题是,当我的应用程序中的会话过期时,我将用户重定向到登录页面,但是如果用户尝试访问在 iframe 弹出窗口中打开的功能而不是重定向到登录页面,我的弹出窗口将打开并显示登录页面,否则它会正常工作。那么检查会话是否过期并将用户从 iframe 弹出窗口内部重定向到登录页面的最佳方法是什么。打开 iframe 弹出窗口的代码如下:

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');
}

回答by Amit

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
  }

回答by Sukesh Marla

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

如果在 IFra 中打开登录页面,(window!=window.top) 将返回 true

Hope it helped

希望有帮助