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

后端 未结 3 1448
深忆病人
深忆病人 2020-11-22 12:05

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

相关标签:
3条回答
  • 2020-11-22 12:40

    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)
    
    0 讨论(0)
  • 2020-11-22 12:46

    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.

    0 讨论(0)
  • 2020-11-22 12:48

    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.

    0 讨论(0)
提交回复
热议问题