一、Web端开发时,用户登录后往往会通过Session来保存用户信息,Session存放在服务器,当用户长时间不操作的时候,我们会希望服务器保存的Session过期,这个时候,因为Session中的用户信息取不到了,就需要用户重新登录,重新保存Session。

Web在登出的时候可以通过HttpSession.Invalidate()//使所有Session作废

Asp.net MVC提供了过滤器,让我们可以很方便的控制访问Action时要处理的事情,针对Session过期后页面跳转,我们可以封装一下Controller的OnActionExecuting方法作为基Controller,如下:

public class BaseController : Controller
{
protected User UserInfo
{
set
{
Session["UserInfo"] = value;
} get
{
if (Session["UserInfo"] == null)
{
return null;
}
else
{
return (User)Session["UserInfo"];
}
}
} protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
#region Session判断
if (UserInfo==null && !filterContext.ActionDescriptor.ActionName.Contains("Login"))
{
filterContext.Result = new RedirectResult("/Home/Login");
return;
}
#endregion base.OnActionExecuting(filterContext);
}
}

  但是,这儿的new RedirectResult("/Home/Login");只是把Action的返回指向为了/Home/Login,如果用户操作的页面是嵌套在iframe中,这个时候,只是iframe的指向改变了,问不是地址栏的指向改变了,针对这种情况,可在前台页面/Home/Login做限制,如下:

@{
Layout = "~/Views/Shared/_Layout.cshtml";
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>系统-登录</title>
<link href="/Content/login.css" rel="stylesheet" /> <script type="text/javascript">
$(function () {
//判断一下当前是不是做顶层,如果不是,则做一下顶层页面重定向
if (window != top) {
top.location.href = location.href;
}
});
</script>
</head>
<body>
</body>
</html>

参照如下:http://blog.csdn.net/u012251421/article/details/50332513  

二、在asp.net mvc我们在记录日志的时候,经常会考虑记录访问者的ip地址,即客户端的ip地址,以下是一个参考的获取ip地址的方式:

/// <summary>
/// 获取web客户端ip
/// </summary>
/// <returns></returns>
public static string GetWebClientIp()
{ string userIP = "未获取用户IP"; try
{
if (System.Web.HttpContext.Current == null
|| System.Web.HttpContext.Current.Request == null
|| System.Web.HttpContext.Current.Request.ServerVariables == null)
{
return "";
} string CustomerIP = ""; //CDN加速后取到的IP simone 090805
CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
if (!string.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
} CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (!String.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
} if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (CustomerIP == null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
}
else
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
} if (string.Compare(CustomerIP, "unknown", true) == || String.IsNullOrEmpty(CustomerIP))
{
return System.Web.HttpContext.Current.Request.UserHostAddress;
}
return CustomerIP;
}
catch { } return userIP; }

参考地址:http://www.cnblogs.com/purplefox2008/p/5983957.html

MVC 访问IFrame页面Session过期后跳转到登录页面的更多相关文章

  1. layui弹窗里面 session过期 后跳转到登录页面

    1.在登录页面添加 <script> $(function () { if (top != window) { layer.msg("登录失效", {icon: 5}) ...

  2. 在子页面session过期无法跳转到父页面

    当session过期后可以用过滤器来设置重定向页面 public class ActionFilter extends HttpServlet implements Filter { private ...

  3. mvc ajax访问后台时session过期无法跳转到Login页面问题解决

    public class BaseController : Controller { protected User UserInfo { set { Session["UserInfo&qu ...

  4. [经验] 项目中 session 过期后弹出的登录窗口无法登录怎么办

    背景: 当session过期后, 按照 系统的设计,  会自动跳转到登录页面, 重新进行登录操作 问题: 由于进入主页后, 其他页面都是嵌入式的模板页, 所以这时的登录页面也是内嵌在index模板下的 ...

  5. 前端跳转处理--房天下的访问页面部分ip自动跳转到登录页面的解决办法(xjl456852原创)

    朋友说自己在访问房天下的页面时,他们页面进行了跳转,跳转到登录页面,说是前端跳转.让我也看看,我看我的机器没有进行跳转. 后来就发现有的机器在访问页面会自动跳转到登录页面.有的不会进行跳转. 比如访问 ...

  6. session失效后跳转到登陆页面

    一.编写Filter拦截器类 package com.pv.utils; import java.io.IOException; import java.io.PrintWriter; import ...

  7. ajax Session失效如何跳转到登录页面

    在Struts应用中,我们发出的请求都会经过 相应的拦截器进行相关处理,一般都会有一个用户登录拦截(Session失效拦截):一般请求的话,如果Session失效时,我们会跳到登录页面,可是如果我们采 ...

  8. 如果后台用framset框架布局,session过期,整个跳出回 登录页面的方法

    如果session过期了,登录页面会在framset框架的右边显示,只能用 js 来做,让整个框架跳出去: 然而,这里 js 必须要用“top”才可以,作用是让整个framset都跳转,直接用 win ...

  9. SpringMVC配置session过期拦截器,返回登录页面

    spring-mvc.xml配置 <mvc:interceptors> <!-- session失效拦截器 --> <mvc:interceptor> <!- ...

随机推荐

  1. The 11 advantages of Java -Why you choose this language

    Java is never just a language.There are lots of programming languages out there, and few of them mak ...

  2. IIS 发布 异常信息 AspNetInitClrHostFailureModule 的解决办法

    昨天在一个客户那里使用Server 2008服务器配置IIS,都配置好之后竟然出现了错误信息,以前没有遇到过 "AspNetInitClrHostFailureModule",于是 ...

  3. UE4 VR 模式画面扭曲 解决方法

    后期处理盒子 详细设置->setting->Misc->screen percentage 设置为100

  4. <a>标签的用法。

    1.创建电子邮件链接: <html> <head> <title>发给朱永成</title> </head> <body> &l ...

  5. POJ 1101 简单BFS+题意

    The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...

  6. C#中的选择语句

    一.选择语句 if,else if是如果的意思,else是另外的意思,if'后面跟()括号内为判断条件,如果符合条件则进入if语句执行命令.如果不符合则不进入if语句.else后不用加条件,但是必须与 ...

  7. Xcode 6制作动态及静态Framework

    技术交流新QQ群:414971585 有没有写SDK或者要将一些常用的工具类做成Framework的经历? 你或许自己写脚本完成了这项工作,相信也有很多的人使用 iOS-Universal-Frame ...

  8. HDU 1016Prime Ring Problem

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入一个数,给出符合要求的素数环. 经典的dfs遍历. #include<iostream&g ...

  9. Codeforces Round #175 (Div. 2)

    A. Slightly Decreasing Permutations 后\(k\)个倒序放前面,前\(n-k\)个顺序放后面. B. Find Marble 模拟. C. Building Perm ...

  10. UVA-11517 Exact Change(DP)

    题目大意:有n张钞票,面值可能不同.你要买一件东西,可能需要找零钱.问最少付多少钱,并求出最少的钞票张数. 题目分析:定义状态dp(i,w)表示前i张钞票凑成w元需要的最少钞票张数.则状态转移方程为d ...