在整个使用ASP.NET会话状态服务应用交流会 [英] Sharing sessions across applications using the ASP.NET Session State Service

查看:144
本文介绍了在整个使用ASP.NET会话状态服务应用交流会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享两个Web应用程序,无论是托管在同一服务器上之间的会话。一个是.NET 2.0 Web窗体应用程序,另一个是作为.NET 3.5 MVC2应用程序。

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"
      />

在Web窗体应用程序我张贴的会话密钥到MVC应用程序:

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    
}

然后我接受她的MVC应用程序,并尝试使用相同的会话是这样的:

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"];

}

该键被公布,但会议似乎并没有在MVC应用得到加载,即sessionKey为空。我所试图做的可以做什么?

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?

推荐答案

我这样做的:

基本上这个想法是应用程序都使用存储在SQLSERVER本地.NET的sessionState。通过使用相同的计算机密钥,使一个小调整到一个存储过程 - 这两个应用可以共享任何会话密钥和/或形式authenication

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.

这两个应用程序会做这样的事情在他们的web.config中:

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.

这样做的文档:
<一href=\"http://msdn.microsoft.com/en-us/library/ms229862(VS.80).aspx\">http://msdn.microsoft.com/en-us/library/ms229862(VS.80).aspx

命令,将需要运行:
C:\\ Program Files文件(x86)的\\微软的Visual Studio 9.0 \\ VC \\ BIN> aspnet_regsql.exe的-E -ssadd --sstype p -S \\ SQLEX $ P $干燥综合征

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

存储过程(TempGetAppID)调整为:

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)

这篇关于在整个使用ASP.NET会话状态服务应用交流会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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