简体   繁体   English

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

[英]Sharing sessions across applications using the ASP.NET Session State Service

I am trying to share sessions between two web applications, both hosted on the same server.我试图在两个 Web 应用程序之间共享会话,这两个应用程序都托管在同一台服务器上。 One is a .net 2.0 web forms application the other is as .net 3.5 MVC2 application.一个是 .net 2.0 Web 表单应用程序,另一个是 .net 3.5 MVC2 应用程序。

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:在 webform 应用程序中,我将会话密钥发布到 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    
}

I then recieve it in the MVC application and try use the same session like this:然后我在 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"];

}

The key is being posted but the session does not seem to get loaded in the MVC app, ie sessionKey is null.密钥正在发布,但会话似乎没有加载到 MVC 应用程序中,即 sessionKey 为空。 Can what I am trying to do be done?我正在尝试做的事情可以完成吗?

I did it this way:我是这样做的:

Basically the idea is both apps use native .net sessionState stored in sqlserver.基本上这个想法是两个应用程序都使用存储在 sqlserver 中的本机 .net sessionState。 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:这两个应用程序都会在它们的 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.会话状态 db 需要在数据库服务器上设置,两个应用程序都可以看到。

Docs for doing this: http://msdn.microsoft.com/en-us/library/ms229862(VS.80).aspx执行此操作的文档: 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需要运行的命令: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:存储过程 (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)

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.将这两个应用程序作为一个虚拟目录放在一个通用的 IIS 应用程序下。 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.根据贾斯汀的评论,只是为了澄清选项 2 并不是指进程外会话的 SQL 状态管理。 I mean for you to actually manually manage the shared data for the two sessions, possibly using a database.我的意思是让您实际手动管理两个会话的共享数据,可能使用数据库。

You can use a common Machine key to generate same Session ID inside both applications for a given user.您可以使用公共机器密钥在给定用户的两个应用程序中生成相同的会话 ID。 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.此外,您还应该计划将两个应用程序的会话存储在一个公共存储中,例如 ASP.NET 状态服务或分布式缓存。

You can use NCache distributed cache which takes provides session sharing feature between different applications.您可以使用 NCache 分布式缓存,它提供不同应用程序之间的会话共享功能。 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.您在会话状态设置中为两个应用程序指定相同的应用程序 ID 标签,这允许您共享会话对象,前提是您为两个应用程序生成了相同的会话 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM