在最小 SharePoint 页面中 window.open 之后会话为空
我将令牌存储在会话变量中。我使用 javascript windows.open 函数在新的 ASPX 页面中启动需要此令牌的报告。当这个新页面加载时,HttpContext.Current.Session 为空。
我已经通过在查询字符串中传递令牌来解决这个问题,但是此窗口中的活动需要保持父窗口的会话处于活动状态,并且我不确定会话对象为空对于这种情况意味着什么。
知道为什么使用 javascript 中的 window.open HttpContext.Current.Session 对象将为 null 吗?
编辑:我要补充一点,这是存储在 SharePoint 库中的基本 System.Web.UI.Page,并且 window.open 函数是从 Web 部件调用的。 我认为此页面可能需要从基类继承才能共享正确的上下文。
更新:我已经缩小范围,认为这与 SharePoint 相关。我将访问 Session 对象的代码移至 Web 部件中。如果放入标准 Web 部件页面,该 Web 部件可以正常工作,但我将其添加到仅包含 ScriptManager、SPWebPartManager 和 WebPartZone 的最小页面。代码运行,但会话对象再次为空。我的最小页面缺少一些使 Session 对象可用的东西。
已解决:我需要实现 IRequiresSessionState 接口的最小 ASPX 页面。之后 Session 对象就在那里。
我将信任安德烈,因为他提供了最有用的信息。
I'm storing a token in a session variable. I launch a report that needs this token in a new ASPX page by using the javascript windows.open function. When this new page loads the HttpContext.Current.Session is null.
I have gotten around this by passing the token in the query string but activity in this window needs to keep the session of the parent window active and I'm not sure what the session object being null means for this scenario.
Any idea why the HttpContext.Current.Session object would be null by using window.open from javascript?
EDIT: I will add that this is a basic System.Web.UI.Page stored in a SharePoint library and the window.open function is called from a webpart. I'm thinking that this page may need to inherit from a base class to share the right context.
UPDATE: I've narrowed down that this is related to SharePoint. I moved the code that accesses the Session object into a web part. The web part works fine if put in a standard web part page but I have it added to a minimal page that only contains a ScriptManager, SPWebPartManager and a WebPartZone. The code runs but the session object is again null. My minimal page is missing something that makes the Session object available.
SOLVED: My minimal ASPX page needed to implement the IRequiresSessionState interface. After that the Session object is there.
I'm going to give the cred to Andrey since he offered the most useful information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
我假设您使用的是 IIS 6 或更高版本。
假设您有 2 个不同的网站:
http://site1.yourdomain.com
http://site2.yourdomain.com
可能会发生 2 件事
两个网站在同一个应用程序池下运行:两个网站的会话应该相同。
注意: 如果最新窗口不是源自当前打开的窗口,则版本 8 之前的 Internet Explorer 将被分配不同的会话。从版本 8 开始,访问同一应用程序池的所有窗口都共享相同的版本,无论窗口的来源如何。
站点在 IIS 中的不同应用程序池下运行:甚至梦想不到您可以为两个窗口共享相同的会话
如果两个窗口的网站相同,那么您在两个窗口之间共享会话应该不会有任何问题,即使使用任何版本的 Internet Explorer,因为第二个窗口是通过调用 window.open() 方法从第一个窗口发起的。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
从技术上讲,这是与网站的不同连接,这就是为什么它是不同的会话。如果您希望不同的窗口使用相同的会话存储,那么最好使用应用程序缓存而不是会话。
更新:
如果您想坚持使用会话状态,您可以做的就是将会话 ID 写入持久 cookie,这样子窗口对服务器的调用将携带它,您可以从该 cookie 检索 SessionID。重要提示:请确保在放入 cookie 之前对会话 ID 进行加密,以避免会话劫持。
Technically, it's a different connection to the web site, that's why it's a different session. It's probably better to use Application cache instead of session if you want different windows to utilize the same session storage.
UPDATE:
What you can do if you want to stick to using session state, is to write the session ID to a persistent cookie, this way the child window's call to the server will carry it along and you can retrieve SessionID from that cookie. IMPORTANT: Make sure you encrypt session ID ebfore putting into teh cookie to avoid session hijacking.