修改密码后,删除session跳转登录页面,登录页面显示在iframe中

问题:修改密码后,删除session跳转登录页面,登录页面显示在iframe中
在这里插入图片描述
解决

原先是这样的:
// 定时跳转
setTimeout("javascript:location.href='../../UserCheckLoginServlet?state=logout'", 2100);

// 定时跳转
setTimeout("javascript:top.location.href='../../UserCheckLoginServlet?state=logout'", 2100);
好的,下面是前后端代码实现的思路: 后端代码: 1. 在BaseController添加一个OnActionExecuting方法,用于判断用户是否登录,如果未登录,则跳转登录页面。 ```csharp public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { HttpSessionStateBase session = filterContext.HttpContext.Session; if (session["UserId"] == null) { filterContext.Result = new RedirectResult("~/Account/Login"); } base.OnActionExecuting(filterContext); } } ``` 2. 在需要登录验证的Controller,继承BaseController即可。 ```csharp public class HomeController : BaseController { public ActionResult Index() { return View(); } } ``` 前端代码: 1. 在前端页面,使用`setInterval()`函数定时向后端发送一个心跳请求,以保持会话的活跃状态。 ```javascript setInterval(function () { $.ajax({ url: "/HeartBeat", type: "get", dataType: "json", success: function (data) { //处理返回的数据 }, error: function () { //处理请求失败的情况 } }); }, 300000); //5分钟发送一次请求 ``` 2. 在前端页面,使用`window.addEventListener()`函数监听所有的ajax请求,如果返回的状态码为401,则跳转登录页面。 ```javascript window.addEventListener('load', function () { $(document).ajaxComplete(function (event, xhr, options) { if (xhr.status == 401) { window.location.href = '/Account/Login'; } }); }); ``` 3. 在前端页面,如果页面嵌套了iframe,则在iframe加载完成后,判断iframe的页面是否需要登录验证,如果需要,则向iframe发送一个心跳请求。 ```javascript function checkIframeLogin() { var iframe = document.getElementById('myIframe'); if (iframe) { var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; var iframeUrl = iframeDoc.location.href; if (iframeUrl.indexOf('NeedLogin') != -1) { setInterval(function () { iframe.contentWindow.postMessage('HeartBeat', '*'); }, 300000); //5分钟发送一次请求 } } } ``` 在iframe的页面,添加一个监听事件,如果收到了来自父页面的心跳请求,则返回一个状态码,用于前端页面跳转。 ```javascript window.addEventListener('message', function (event) { if (event.data == 'HeartBeat') { $.ajax({ url: '/HeartBeat', type: 'get', dataType: 'json', success: function (data) { //处理返回的数据 }, error: function () { parent.window.location.href = '/Account/Login'; } }); } }); ``` 上述代码,`parent.window.location.href = '/Account/Login';`表示在父页面跳转登录页面。 希望以上代码可以帮助到您,如有任何疑问,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值