我试图在两个web应用程序之间共享会话,这两个应用程序都托管在同一台服务器上.一个是a.net 2.0 web表单应用程序,另一个是as.net 3.5 MVC2应用程序.

两款应用的会话设置如下:

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

在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    
}

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

[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为空.我想做的事能做到吗?

推荐答案

我是这样做的:

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

这两款应用都会在他们的网络上做类似的事情.配置:

<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

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)调整为:

 @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相关问答推荐

目前不支持超过 65534 个段的跨区存档

检测 ASP.NET 中的内存泄漏

无法连接到 ASP.Net 开发服务器问题

如何将 JQuery 与母版页一起使用?

ASP.NET Web 应用程序 (.NET Framework) 与 ASP.NET Core Web 应用程序 (.NET Framework) 之间的差异

Azure Service Fabric 是否与 Docker 做同样的事情?

返回 HttpResponseMessage 时的 WebAPI Gzip

模型项的类型为 CookMeIndexViewModel,但需要类型为 IEnumerable 的模型项

如何将 Castle Windsor 与 ASP.Net Web 表单一起使用?

多行文本框多个换行符

将 ListItem 的 ASP.NET 列表数据绑定到 DropDownList 问题

System.Linq.Dynamic 不支持 OrderByDescending("someColumn")?

asp.net cookie、身份验证和会话超时

为什么对 ASP.NET MVC 控制器的调用不执行 DelegatingHandler?

当.NET抛出WebException((400)错误请求)时如何处理WebResponse?

为什么 HttpWebRequest 会抛出异常而不是返回 HttpStatusCode.NotFound?

MVC 模型布尔显示是或否

Ashx 文件中的 HttpContext.Current.Session 为空

Sys.WebForms.PageRequestManagerServerErrorException:处理服务器上的请求时发生未知错误.

System.Drawing.Image.FromFile() 上的内存不足异常