gpt4 book ai didi

c# - 在两个 asp.net web 应用程序之间共享相同的 SessionId

转载 作者:行者123 更新时间:2023-11-30 17:07:49 25 4
gpt4 key购买 nike

我在两个 Web 应用程序之间使用了 sql server session 模式。 session ID 在 ASPState 数据库中创建。我需要在不使用查询字符串和 cookie 的情况下将 session 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 数据库中获得了 session key 。但是如何从相同的 session ID 传递给第二个应用程序?

最佳答案

您可以使用 SessionManager 将 ID 设置为您在目标站点中想要的任何值。您只需要通过查询字符串 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

关于c# - 在两个 asp.net web 应用程序之间共享相同的 SessionId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14254737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com