记录一次Session偶尔获取不到的解决过程

导读

平台下某子系统有密码登录需求,初步考虑用Session,登录后设置Session[key]=value;Session中若某key对应的Session,即Session[key]为null则限制操作。对应所用技术点:Asp.Net MVC & Razor 视图引擎,技术点较简单,虽然走了一些弯路,但加深了对Session的理解。

一、前台ajax校验输入密码正确性

$.ajax({
            url: "@Url.Action("CheckPwd","Earnings")",
                type: "post",
                data: { "pwd": pwd},
                beforeSend: function () {
                    loadingLayer = layer.load(1, {
                        shade: [0.6, '#fff']
                    });
                },
                success: function (data) {
                    var result = JSON.parse(data);
                    if (result.Success) {
                       //页面跳转
                    }
                    else {
                       //
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                  //
                }
            });
        }

二、后台Action 校验密码输入正确后,设置Session["EarningsManagePwd"] 

public ActionResult CheckPwd(string pwd)
        {
            if (//)
            {
                Session["EarningsManagePwd"] = encryptPwd;
            }
            return Json();
        }

三、在其他页面,未登录没有访问权限时,跳转到登录页,后台Action核心代码如下:

if (Session["EarningsManagePwd"] == null || string.IsNullOrEmpty(Session["EarningsManagePwd"].ToString()))
            {
                return RedirectToAction("Index");
            }

四、出现问题,Session值时而获取不到

  • 做的一些demo,简单使用Session,没有发现异常;
  • 本地调试没发现什么问题,发布到服务器后,发现Session["EarningsManagePwd"]的值时而获取不到,变为null,操作页面时总跳转到登录页,多次操作中偶尔会获取到Session值,达到预期效果;
  • 然而这样的效果坚决不能容忍,势必找寻其根源,探究哪里出了问题;

五、查询解决方案历程

 1.略过一系列调试步骤及“不对症的药方”,怀疑System.Web.HttpContext.Current.Session与MVC下Session二者的区别

(1)MVC下Session转到定义

(2)System.Web.HttpContext.Current.Session转到定义

两者是同一个类,似乎没有本质区别

(3)做兼容处理,判断二者的值,二者也是同一个值,仍旧时而获取不到值,时而获取到

(4)HttpContext.Current.Session 和 Session 的区别(后话)

HttpContext.Current.Session 和 Session 的区别

原文地址:http://stackoverflow.com/questions/7954844/what-is-the-difference-between-these-two-httpcontext-current-session-and-session

What is the difference between these two HttpContext.Current.Session and Session - asp.net 4.0

 

 

They're effectively the same, in that they will access the same Session data.

他们具有相同的作用,他们将会访问相同的session数据。

The reason you can call Session in your code-behind is because ASP.Net pages by default extend the System.Web.UI.Page type. This has a Session public property. If you look at the code for this in Reflector you can see that it just calls HttpContext.Current.Session itself (through its own Contextproperty).

 

在你的code-behind代码中之所以能够调用session,是因为Asp.net的页面默认是继承自system.web.ui.page类型的。这个类型有一个名字为session的公共属性。如果你在Reflector反编译软件中查看这个公共属性的代码,你会看到这个属性自己仅仅是调用的httpcontext.current.session而已。(在他自己的 Context属性中也全部是这样使用的)

 

In other classes you will not have access to that property, but you can use HttpContext.Current.Session to access the session data instead, as long as you're running in the context of a web application.

在其他的类中,你可能不可以直接访问system.web.ui.page的session属性,但是你能够用httpcontext.current.session来访问session数据,只要你的代码正运行在一个web应用程序的上下文中。

2.查询到可以通过更改Web.config文件中sessionState节点配置来解决

(1)C#SESSION丢失问题的解决办法

我们在用C#开发程序的时候经常会遇到Session很不稳定,老是数据丢失。下面就是Session数据丢失的解决办法希望对您有好处。

1、在WEB.CONFIG文件中修改SESSION状态保存模式,如:<sessionState mode='StateServer' stateConnectionString='tcpip=127.0.0.1:42424' sqlConnectionString='data source=127.0.0.1;Trusted_Connection=yes' cookieless='true' timeout='180'/> 

2、启动系统服务“ASP.NET状态服务 ”,系统默认是手动启动的 

3、如果SESSION中保存的数据类型是自定义的,如结构,请在自定义数据类型处序列化会话状态,即在类或结构申明前加[Serializable] 

启动Asp.Ner State Service

将web.config文件中sessionState节点替换为”<sessionState mode='StateServer' stateConnectionString='tcpip=127.0.0.1:42424' sqlConnectionString='data source=127.0.0.1;Trusted_Connection=yes' cookieless='true' timeout='180'/> ”,Session["EarningsManagePwd"]取值正常,达到预期效果,然而出现了新的问题,浏览器地址栏莫名多了如下一串字符串,每个链接地址下都多了类似的字符串

多余字符串

(2)经历一番无用研究,找到可用的针对上述问题的解决方案

session标识两种方式存储:
  1)cookieless=false,默认方式,sessionId存放在cookie
  2)cookieless=true,sessionId存放在url

常用的sessionstate模式:
1.inProc模式:受IIS进程影响,容易丢失,但是也正因如此,是唯一支持Session_OnEnd 事件的模式;
2.StateServer模式 :提供独立的状态服务,即使web应用程序重启,会话仍然保持;
3.SQLServer 模式:利用sqlserver提供状态存储,即使web应用程序重启,会话仍然保持,
   并且,利用故障转移集群,可以让其他sqlserver接管故障服务,而不会丢失session

将web.config文件中sessionState节点cookieless属性值改为false,地址栏sessionId果然消失了!

(3)优化sessionState 配置节点(暂不考虑Session集群共享)

<sessionState mode="StateServer" ></sessionState>

附其他参考资料

1.理解Session的集中模式

2.sessionState元素(ASP.Net 设置架构)

总结

解决Session偶尔获取不到的方法

1.web.config文件中sessionState节点mode属性由默认的InProc改为StateServer;

2.启动服务Asp.Net State Service;

3.如果Session中保存的数据类型是自定义的,如结构,请在自定义数据类型处序列化会话状态,即在类或结构申明前加[Serializable] ;

posted @ 2017-11-16 17:03  负了韶华不负卿  阅读(8783)  评论(7编辑  收藏  举报