使用ASP.NET WebForms记住iframe中的会话 [英] Remembering Session in iframe with ASP.NET WebForms

查看:96
本文介绍了使用ASP.NET WebForms记住iframe中的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个旧的WebForms应用程序中发生了奇怪的行为.我不能给您链接或全景图,我只能询问具体细节,所以也许有人对探索的方式有所了解.

Strange behavior occured in an old WebForms app. I can't give you link, or big picture, I can only ask for specific detail, so maybe someone has an idea which way to explore.

App会将应用嵌入到iframe中的另一个域B上.(如果它在相同的域上托管站点,那很好,就不会出现此问题.)

App on the domain A embeds app on another domain B within iframe. (If it hosts the site on the same domain, then it's fine, the problem doesn't occur.)

domainA.com:
<iframe src="http://domainB.com/page1.aspx" />

page1.aspx写入一个Session变量:

page1.aspx writes a Session variable:

http://domainB.com/page1.aspx:
HttpContext.Current.Session["UTCOffset"] = utcOffset.ToString();

但是,该iframe中的ASHX处理程序handler.ashx无法读取会话变量值.

However, ASHX handler handler.ashx within that iframe can't read the Session variable value.

http://domainB.com/handler.ashx:
string utcOffset = Convert.ToString(HttpContext.Current.Session["UTCOffset"]);

utcOffset是空字符串.

utcOffset is empty string.

请记住,当它不在iframe中时(例如,请求 http://domainB.com/page1.aspx (通过浏览器)).如果它们都托管在同一个域中,它也可以正常工作.直到最近,它在大多数主流浏览器上都可以正常工作.

Have in mind, when this is not inside iframe, (e.g. request http://domainB.com/page1.aspx through browser) this works fine. It also works fine if this is all hosted on the same domain. Also it was working fine until recently, on most major browsers.

由于CORS或其他原因,最近有关框架内会话策略的更改是否发生了变化?我该如何解决?

Did something change recently regarding Session policy within frames, due to CORS or something? How do I fix this?

推荐答案

我只是通过使用cookie来解决它.经过测试,效果很好.

I solved it simply by using cookies. Tested it, it works fine.

因此,而不是page1.aspx中的代码:

So instead of the code in page1.aspx:

            HttpCookie UTCOffset = new HttpCookie("UTCOffset");
            UTCOffset.Values.Add("utcOffset", utcOffset.ToString());
            UTCOffset.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(UTCOffset);

和处理程序中,而不是当前代码中:

and in handler, instead of current code:

        string utcOffset = string.Empty;
        HttpCookie UTCOffset = HttpContext.Current.Request.Cookies["UTCOffset"];
        if (UTCOffset != null && !string.IsNullOrEmpty(UTCOffset.Values["utcOffset"]))
        {
            utcOffset = UTCOffset.Values["utcOffset"].ToString();                
        }

这篇关于使用ASP.NET WebForms记住iframe中的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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