重定向后,ASP.NET会话丢失,但仅限于IE [英] ASP.NET session lost after redirect but only with IE

查看:95
本文介绍了重定向后,ASP.NET会话丢失,但仅限于IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有斜体均为原始帖子,下面的编辑均非斜体

我正在使用ASP.NET 4.0用C#编写.我通过SQL查找对用户凭据进行身份验证,如果有效,则将用户名存储在会话变量中,然后将用户重定向回主页.很简单.

if (!db.isValidLogin(userName, passWord))
    {
        //invalid login, show it!
        //just some code to tell the user invalid credentials
    }
    else
    {
        //show login successful!
        //update some items on the screen
        Session["username"] = userName.ToUpper();
        Response.Redirect("/");
    }

由于这是内部开发,因此尚未通过SSL.当我使用Chrome版本"25.0.1364.172 m"时,我已正确重定向,并且已登录".我的屏幕通过显示我的用户名并允许我访问身份验证允许的功能来代表该屏幕.当我使用具有相同服务器端代码和过程的(32位)IE 9版本"9.0.8112.16421" ...当我执行重定向时,会话变量用户名"消失了.实际上,会话的项目计数为0.在重定向之前,会话变量已设置且正确.在Windows Server 2008 R2 64位盒和Windows 7 64位盒上,我得到相同的结果.我正在使用托管IIS和SQL的单个服务器.我没有使用会话服务器.我已经找到了...代码一直在按期望的方式运行,直到重定向为止.接收凭据,执行我的存储过程以进行验证...在重定向之前设置会话变量(我可以看到会话和变量,并且值是正确的..),然后进行重定向...如前所述,使用Chrome,它可以正常工作根据需要...使用IE,会话在重定向时丢失.我也尝试过此方法,但没有成功:Response.Redirect("/",false);因此,我深信IE正在执行某些操作,可能是在客户端上设置了cookie,这导致了浏览器和服务器会话之间的不匹配.我不应该做一个response.redirect吗???如果我执行response.redirect,如何避免会话重置?再次提醒您,当我使用Chrome时不会发生这种情况.令人沮丧...感谢您的帮助!

新信息

尝试关闭每个答案的IE缓存后...我决定将sessionID输出到浏览器,以便可以看到它的含义.

After attempting to turn off IE caching per an answer... I decided to output the sessionID to the browser so I could see what it was.

该行为比登录和重定向更直接...

The behavior is more direct that the login and redirect...

在IE中,只需使用F5刷新浏览器即可在服务器上创建一个新会话.每次刷新时,我都会收到一个新的会话ID.

In IE simply refreshing the browser with F5 causes a new session to be created on the server. Each refresh I receive a NEW session ID.

使用Chrome进行测试,除非调用session.abandon,超时或关闭并重新启动浏览器,否则我不会获得新的会话ID.

Testing this with Chrome I do not get a new session ID unless I call session.abandon, timing out my session or closing and restarting the browser.

当用户单击注销时,我只是在调用session.abandon,但是注释了该代码(以防万一),以确保我不会在偶然的情况下放弃它.

I was only calling session.abandon when the user clicked log out, but have commented out that code (just in case) to ensure that I'm not abandoning it on accident.

实际页面刷新之间的某个位置,IE正在将自身呈现给服务器以进行新的会话……ARGH.

Somewhere between actual page refreshes IE is presenting itself to the server for a new session... ARGH.

例如:铬合金:登录之前:myjuzrmccerk1t4eakcliq14登录后:myjuzrmccerk1t4eakcliq14

For example: Chrome: Before login: myjuzrmccerk1t4eakcliq14 After login: myjuzrmccerk1t4eakcliq14

IE:登录之前:unyebuc2ikac12xnhpssy0em登录后:unyebuc2ikac12xnhpssy0em

IE: Before login: unyebuc2ikac12xnhpssy0em After login: unyebuc2ikac12xnhpssy0em

使用F5或Ctrl-R刷新:一:ptjt42fjwzgdreyyyo3cmvrs二:s1hd5aatl5yexeuc125aqhst三:kbpflurcdcxubux3scmdm4k5

Refreshes with F5 or Ctrl-R: one: ptjt42fjwzgdreyyyo3cmvrs two: s1hd5aatl5yexeuc125aqhst three: kbpflurcdcxubux3scmdm4k5

更新2

我已将站点更改为使用状态服务器"进行会话,并启动了适当的服务...行为没有变化.

I have changed the site to use "State Server" for the session and started the appropriate service... There is no change in behavior.

答案

由于我的代表不高...这不允许我再回答我自己的问题3个小时...但是这里是..

Since my rep is low.. .this won't let me answer my own question for another 3 hours... but here it is..

我通过反复试验找到了解决方法.

I found a fix... through trial and error.

处于会话状态的InProc和StateServer都具有相同的结果,直到我添加了"cookieless = true"

InProc and StateServer in sessionstate both had the same results until I added "cookieless=true"

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

这将导致会话状态在Chrome和IE(问题所在)中保持一致,并且我的会话ID在页面刷新之间不再更改.我无法确定为什么会这样,但是仍然可以解决..感谢Mike和antinescience的帮助!

This causes the session state to be consistent in both Chrome and in IE (where the problems was) and my session ID no longer changes between page refreshes. I was unable to determine WHY this happens, but it is fixed nonetheless.. Thanks Mike and antinescience for your help!

推荐答案

会话状态中的InProc和StateServer都具有相同的结果,直到我添加"cookieless = true"

InProc and StateServer in sessionstate both had the same results until I added "cookieless=true"

这将导致会话状态在Chrome和IE(问题所在的地方)中均保持一致,并且在刷新页面之间我的会话ID不再更改.我无法确定为什么会这样,但是仍然可以解决..感谢Mike和antinescience的帮助!

This causes the session state to be consistent in both Chrome and in IE (where the problems was) and my session ID no longer changes between page refreshes. I was unable to determine WHY this happens, but it is fixed nonetheless.. Thanks Mike and antinescience for your help!

这篇关于重定向后,ASP.NET会话丢失,但仅限于IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆