在Web应用程序共享SQL Server会话状态 [英] Sharing SQL Server Session State across Web Applications

查看:228
本文介绍了在Web应用程序共享SQL Server会话状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设立的SQL Server会话状态的一个非常基本的演示,但我有一些麻烦它的工作。我想测试这一点在本地运行Windows 7与IIS 7.5和SQL Server 2008 R2。

I'm setting up a very basic demo of SQL Server Session State, but am having some trouble getting it working. I'm trying to test this out locally running Windows 7 with IIS 7.5 and SQL Server 2008 R2.

<强>最后,我需要一种方法来跟踪登录到系统,该系统是负载平衡的几个不同的web服务器之间的用户数。所以,我需要或缩小来更新会话变量(存储在SQL)每次用户登录。因此,会话ID可能永远是不同的。这可能吗?

下面就是我迄今所做的:

Here's what I've done so far:


  1. 在IIS(1和Site2)创建两个新的网站

  2. 创建两个新的Web应用程序项目在VS 2010中(1和Site2)

  3. 在这两个网站,Default.aspx页面上,我创建了一个按钮,点击会救不站点1或站点2的时候(具体取决于网站)到名为LastSiteUsed会话变量。还有一个按钮,在点击后会从LastSiteUsed会话变量读取的值,并在标签中显示它。

  4. 在SQL中,我创建了一个名为dbSessionTest新的数据库。然后我也跟着在 MSDN的说明安装使用aspnet_regsql工具

  5. 在我的两个Web应用程序的Web.config文件中,我已经添加在&LT以下;&System.Web程序GT;

  1. Created two new sites in IIS (Site1 and Site2)
  2. Created two new Web Application projects in VS 2010 (Site1 and Site2)
  3. In both sites, on the Default.aspx page, I created a button that when clicked will save either "Site1" or "Site2" (depending on the site) into a session variable called "LastSiteUsed". There's another button, that when clicked, will read the value from the "LastSiteUsed" session variable and display it in a label.
  4. In SQL, I created a new Database called dbSessionTest. Then I followed the instructions on MSDN to install the Session State Database using the aspnet_regsql tool.
  5. In both of my Web Application's Web.config file, I've added the following under <System.Web>:

&LT;的sessionState模式=SQLServer的sqlConnectionString =服务器= \\ sqlex preSS;数据库= dbSessionTest; UID =名为myUsername; PWD = MYPASSWORD无Cookie =假超时= 20allowCustomSqlDatabase =真/&GT;

这两个网站正常,但他们似乎并不为共享的会话。例如,当我点击我的按钮,保存站点1我的会话变量,我期望能够读取Site2的那个值,但它不工作。站点2将只能返回站点2的站点1将只返回站点1。

Both sites function normally, but they don't seem to be sharing the session. For example, when I click my button to save my session variable from Site1, I'd expect to be able to read that value from Site2, but it doesn't work. Site2 will only return "Site2" and Site1 will only return "Site1".

有没有人有什么我可以做错了任何想法?我错在想,我应该能够读取站点1从Site2的设置值?

Does anyone have any ideas on what I could be doing wrong? Am I wrong in thinking that I should be able to read a value set by Site1 from Site2?

更新:

我可以看到会话数据得到存储在 ASPStateTempSessions 表中的SQL Management Studio中,但每个网站仍然只能看到它写的值。这两个站点都设置会话变量:

I can see that session data is getting stored in the ASPStateTempSessions table in SQL Management Studio, but each site can still only see the value which it wrote. Both sites are setting the session variable like this:

Session("LastSiteUsed") = "Site2"

和这两个网站都像检索值:

And both sites are retrieving the value like:

lblValue.Text = "Value from Session: " & Session("LastSiteUsed").ToString

我是否需要访问存储在SQL Server会话变量不同的做到这一点?

Do I need to do this differently for accessing session variables stored in SQL Server?

更新2:

我使用的是默认的数据库试过,ASPState的,这是由运行此命令创建的:

I've tried using the default database, ASPState, which is created by running this command:

aspnet_regsql.exe -S MyServerName -E -ssadd -sstype p

然后简化我的每个Web.config文件中,如:

Then simplified each of my Web.config files like:

<sessionState mode="SQLServer" sqlConnectionString="Data Source=.\sqlexpress;User ID=myUsername;Password=myPassword" cookieless="false" timeout="20"/>

但同样,没有运气。我希望能够从站点1设置会话变量,然后读取Site2的价值,但它不工作。同样,我能看到的条目显示了在 ASPStateTempSessions 表,所以我知道他们要正确输入。有一件事我注意到的是,他们有不同的会话ID?

But again, no luck. I want to be able to set a session variable from Site1 and then read the value from Site2, but it's not working. Again, I'm able to see entries showing up in the ASPStateTempSessions table so I know they're getting entered correctly. One thing I noticed is they have different session IDs?

有什么我需要做不同的,以确保相同的会话ID设置/读取相同的会话变量时,我的站点之间使用?

Is there something I need to be doing differently to ensure the same session ID is used between my sites when setting/reading the same session variable?

更新3:

我已经按照从 <一的说明href=\"http://www.stionasoftware.com/Blogs/post/2010/10/27/Sharing-NET-Session-across-multiple-applications.aspx\"相对=nofollow>这篇文章 它修改了SP和分组到 ASPStateTempApplications 表中添加一列。这的一种的工作,但只有当我的两个网站都是在同一个浏览器中打开(使用标签)的。我需要能够在IE中打开站点1,值保存到我的会话变量,关闭浏览器,打开站点2在Chrome中,并读取该值。从一切我读过与SQL Server会话状态......这应该是可能的。

I've followed the instructions from this article which modifies an SP and adds a column for grouping to the ASPStateTempApplications table. This kind of worked, but only if both of my sites were open in the same browser (using tabs). I need to be able to open Site1 in IE, save a value to my session variable, close the browser, open Site2 in Chrome, and read the value. From everything I've read with SQL Server Session State... this should be possible.

更新4 - 解决方案:

我也跟着上的 <一个答案href=\"http://stackoverflow.com/questions/2868316/sharing-sessions-across-applications-using-the-asp-net-session-state-service/3151315#3151315\">this文章 按@SilverNinja提供。通过在命令提示符下我重新创建 ASPState的数据库(撤消在更新#3我的SP的变化),然后修改 TempGetAppID SP按照从链接答案。我也更新我的两个Web.config文件中,从该文章的答案匹配,以及:

I followed the answer on this article provided by @SilverNinja. I re-created the ASPState database via the command prompt (to undo my SP changes in Update #3) and then modified the TempGetAppID SP as per the answer from the link. I've also updated both of my Web.config files to match the answer from that article as well:

<sessionState mode="SQLServer"
              sqlConnectionString="Data Source=.\sqlexpress;User ID=myUsername;Password=myPassword;Application Name=CacheTest"/>

我也包含在这两个Web.config文件中相同的计算机密钥:

I've also included identical Machine Keys in both Web.config files:

<machineKey validationKey="59C42C5AB0988049AB0555E3F2128061AE9B75E3A522F25B34A21A46B51F188A5C0C1C74F918DFB44C33406B6E874A54167DFA06AC3BE1C3FEE8506E710015DB" decryptionKey="3C98C7E33D6BC8A8C4B7D966F42F2818D33AAB84A81C594AF8F4A533ADB23B97" validation="SHA1" decryption="AES" />

现在我能够(使用IE)打开我的两个网站,设置为从站点1的值,并且从站点2(反之亦然)阅读。当我检查表中,只有一个SessionID的存在(这两个网站被正确使用同一个会话)。我错在打开一个新的浏览器(例如,Chrome浏览器)之前我的想法,将使用相同​​的会话 - 一个新的浏览器将开始它自己的会话。虽然上一个负载平衡的情况下,这应该不会造成任何问题。

Now I am able to (using IE) open up both of my sites, set a value from Site1, and read it from Site2 (and vice versa). When I check the table, only one SessionID exists (both sites are using the same session correctly). I was wrong in my thinking before that opening a new browser (for example, with Chrome), would use the same session - a new browser will start it's own session. On a load-balanced scenario though, this shouldn't cause any issues.

推荐答案

您的问题是,分享会在整个ASP.NET应用程序状态。这是不支持外的开箱即用的SQL Server会话提供商。看到这个 SO张贴关于什么样的变化,使到SQL Server会话提供支持跨应用程序会话的。

The problem you have is sharing session state across ASP.NET applications. This is not supported out-of-the-box with SQL Server session provider. See this SO post regarding what changes to make to SQL Server session provider to support cross-application sessions.

我不会为管理该共享的状态使用会话(即 LastSiteUsed )。该信息应与用户配置文件存储被持久化(的 会员 档案提供商,自定义数据库,等等。 的)。由于会话过期 - 跨应用程序使用共享会话不是一个可靠的机制来持续跟踪用户特定的状态。如果持久性不是关键不是利用href=\"http://msdn.microsoft.com/en-us/library/ff383731%28v=azure.10%29.aspx\"> AppFabric的缓存为

I would not use session for managing this shared state (i.e. LastSiteUsed). This information should be persisted with the user profile store (Membership, Profile provider, custom DB, etc.). Since session can expire - using a shared session across applications isn't a reliable mechanism to track persistent user-specific state. If persistence isn't critical than utilize the AppFabric Cache for an in-memory cross-application shared state.

这篇关于在Web应用程序共享SQL Server会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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