在两个asp.net Web应用程序之间共享相同的SessionId

时间:2013-01-10 09:40:16

标签: c# asp.net sql-server-2008

我在两个Web应用程序之间使用了sql server会话模式。 会话ID在ASPState数据库中创建。我需要在不使用查询字符串和cookie的情况下将会话ID从第一个应用程序共享到第二个应用程序。我按照link的步骤进行操作。下面是代码。
在两个应用(web.config)

<add name="DBConnection" connectionString="Data Source=localhost;DataBase=ASPState;Integrated Security=True;uid=sa;pwd=password-1" providerName="System.Data.SqlClient"/>

<sessionState mode="SQLServer" sqlConnectionString="DBConnection" allowCustomSqlDatabase="true" regenerateExpiredSessionId="false" cookieless="false" timeout="1" ></sessionState>         

<machineKey validationKey="566A547C407CB0C47ABAEC581B8B90C45E15039806352E4611B35E0FB70C1C096350A4BBAE816191331DCA55874D3F96B41FFDED4C6A913591684A90825F53A0" decryptionKey="B299127DFC22648C2EF92D7EDF2E4960277F562C60F3BDE4A4C3BFDFEA602FDF" validation="SHA1" decryption="AES" />
<modules runAllManagedModulesForAllRequests="true">    
<add name="BasicAuthenticationModule" type="UserAuthenticator" />
</modules>

在第一个应用 Home.aspx.cs

protected void imgBTN_Click(object sender, EventArgs e)
{
   string sessionKey = HttpContext.Current.Session.SessionID;
  //How to transfer the same session id to the second application
   Response.Redirect("http://localhost:43392/PartnerHome.aspx"");
}

我从ASPState数据库获取了会话密钥。但是如何从相同的会话ID传递到第二个应用程序?

1 个答案:

答案 0 :(得分:2)

您可以使用SessionManager将ID设置为目标站点中的任何内容。你只需要通过querystring GET或从一个到另一个的POST形式传递它:

来源

Response.Redirect("http://localhost:43392/PartnerHome.aspx?SessID="
    + Session.SessionID);

目标

string sessionId = Request.QueryString["SessId"];
bool redirected = false;
bool isAdded = false;
new System.Web.SessionState.SessionIDManager().SaveSessionID(
    HttpContext.Current, sessionId, out redirected, out isAdded);

请参阅SessionIDManager on MSDN

相关问题