使用 ASP.NET 会话状态服务在应用程序之间共享会话

发布于 2024-09-02 21:40:59 字数 990 浏览 1 评论 0原文

我正在尝试在两个 Web 应用程序之间共享会话,这两个应用程序都托管在同一服务器上。一种是 .net 2.0 Web 表单应用程序,另一种是 .net 3.5 MVC2 应用程序。

两个应用程序的会话设置如下:

<sessionState
      mode="StateServer"
      stateConnectionString="tcpip=127.0.0.1:42424"
      />

在 Web 表单应用程序中,我将会话密钥发布到 MVC 应用程序:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Session["myvariable"] = "dan"; 
    string sessionKey = HttpContext.Current.Session.SessionID;

    //Followed by some code that posts sessionKey to the other application    
}

然后我在 MVC 应用程序中接收它,并尝试使用相同的会话,如下所示:

[HttpPost]
public  void Recieve(string sessionKey )
{
    var manager = new SessionIDManager();

    bool redirected;
    bool IsAdded;

     manager.SaveSessionID(HttpContext.ApplicationInstance.Context, Id, out redirected, out IsAdded);
     var myVar = Session["myvariable"];

}

密钥正在发布,但session 似乎没有在 MVC 应用程序中加载,即 sessionKey 为 null。我想做的事情可以完成吗?

I am trying to share sessions between two web applications, both hosted on the same server. One is a .net 2.0 web forms application the other is as .net 3.5 MVC2 application.

Both apps have their session set up like this:

<sessionState
      mode="StateServer"
      stateConnectionString="tcpip=127.0.0.1:42424"
      />

In the webform application I am posting the the session key to the MVC app:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Session["myvariable"] = "dan"; 
    string sessionKey = HttpContext.Current.Session.SessionID;

    //Followed by some code that posts sessionKey to the other application    
}

I then recieve it in the MVC application and try use the same session like this:

[HttpPost]
public  void Recieve(string sessionKey )
{
    var manager = new SessionIDManager();

    bool redirected;
    bool IsAdded;

     manager.SaveSessionID(HttpContext.ApplicationInstance.Context, Id, out redirected, out IsAdded);
     var myVar = Session["myvariable"];

}

The key is being posted but the session does not seem to get loaded in the MVC app, i.e. sessionKey is null. Can what I am trying to do be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-09-09 21:40:59

我是这样做的:

基本上这个想法是两个应用程序都使用存储在 sqlserver 中的本机 .net sessionState。通过使用相同的机器密钥并对存储过程进行一些小调整 - 两个应用程序可以共享任何会话密钥和/或表单身份验证。

两个应用程序都会在其 web.config 中执行类似的操作:

<sessionState mode="SQLServer" sqlConnectionString="Data Source=.\SQLEXPRESS;User Id=test;Password=test;Application Name=AppName"  />
    <machineKey
validationKey="SOMEKEY"
validation="SHA1" decryption="AES"
/>

会话状态数据库需要在数据库服务器上设置,两个应用程序都可以看到。

用于执行此操作的文档:
http://msdn.microsoft.com/en-us/ library/ms229862(VS.80).aspx

需要运行的命令:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin>aspnet_regsql.exe -E -ssadd --sstype p -S .\SQLEXPRESS

存储过程 (TempGetAppID) 调整为:

 @appId int OUTPUT
AS

    -- start change

    -- Use the application name specified in the connection for the appname if specified
    -- This allows us to share session between sites just by making sure they have the
    -- the same application name in the connection string.
    DECLARE @connStrAppName nvarchar(50)
    SET @connStrAppName = APP_NAME()

    -- .NET SQLClient Data Provider is the default application name for .NET apps
    IF (@connStrAppName <> '.NET SQLClient Data Provider')
        SET @appName = @connStrAppName

    -- end change

SET @appName = LOWER(@appName)

I did it this way:

Basically the idea is both apps use native .net sessionState stored in sqlserver. By using the same machine key and making a small tweak to a stored procedure – both apps can share any session keys and/or forms authenication.

Both apps would do something like this in their web.config:

<sessionState mode="SQLServer" sqlConnectionString="Data Source=.\SQLEXPRESS;User Id=test;Password=test;Application Name=AppName"  />
    <machineKey
validationKey="SOMEKEY"
validation="SHA1" decryption="AES"
/>

Session state db would need to be set up on a database server, that both apps can see.

Docs for doing this:
http://msdn.microsoft.com/en-us/library/ms229862(VS.80).aspx

Command that would need to be run:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin>aspnet_regsql.exe -E -ssadd --sstype p -S .\SQLEXPRESS

Stored procedure (TempGetAppID) tweak to:

 @appId int OUTPUT
AS

    -- start change

    -- Use the application name specified in the connection for the appname if specified
    -- This allows us to share session between sites just by making sure they have the
    -- the same application name in the connection string.
    DECLARE @connStrAppName nvarchar(50)
    SET @connStrAppName = APP_NAME()

    -- .NET SQLClient Data Provider is the default application name for .NET apps
    IF (@connStrAppName <> '.NET SQLClient Data Provider')
        SET @appName = @connStrAppName

    -- end change

SET @appName = LOWER(@appName)
筑梦 2024-09-09 21:40:59

问题在于会话密钥的范围仅限于应用程序,因此具有相同会话密钥的两个应用程序实际上具有不同的会话。

您可以执行以下两种操作之一:

  1. 将这两个应用程序作为虚拟目录放置在通用 IIS 应用程序下。我认为这不是一个好主意,但它会起作用。

  2. 针对您想要共享的数据推出您自己的会话数据解决方案。可能会使用后端数据库作为公共存储(如果您有的话)。

根据 Justin 的评论,只是为了澄清选项 2 并不是指进程外会话的 SQL 状态管理。我的意思是让您实际手动管理两个会话的共享数据,可能使用数据库。

The problem is that session keys are scoped to the applications, so two applications having the same session key in fact have separate sessions.

You can do one of two things:

  1. Put both applications as a virtual directory under a common IIS Application. I don't think this is a good idea, but it will work.

  2. Roll your own session data solution for the data you want to share. Possibly using the backend database as the common storage, if you have one that is.

Based on Justin's comment, just to clarify option 2 is not refering to the SQL state managemet for out of process sessions. I mean for you to actually manually manage the shared data for the two sessions, possibly using a database.

戏舞 2024-09-09 21:40:59

您可以使用通用机器密钥在两个应用程序内为给定用户生成相同的会话 ID。此外,您还应该计划将两个应用程序的会话存储在公共存储中,例如 ASP.NET 状态服务或分布式缓存。

您可以使用NCache分布式缓存,它提供不同应用程序之间的会话共享功能。您可以在会话状态设置中为两个应用程序指定相同的应用程序 ID 标记,这样您就可以共享会话对象,前提是您为两个应用程序生成了相同的会话 ID。

You can use a common Machine key to generate same Session ID inside both applications for a given user. Additionally, you should also plan on storing sessions of both applications in a common store such as ASP.NET State Service or a distributed cache.

You can use NCache distributed cache which takes provides session sharing feature between different applications. You specify same Application ID tag for both apps inside session state settings which allows you to share session object provided you have same Session ID generated for both applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文