asp.net 站点不同应用SQLServer共享Session
在两个应用的web.config 中 system.web 节点 设置使用SQLServer保存Session
1 <sessionState mode="SQLServer" cookieless="false" timeout="1000" sqlConnectionString="Data Source=服务器IP;Persist Security Info=True;User ID=sa;password=123456"/>
然后到数据库服务器中 打开 ASPState 数据库
执行下面的语句
USE [ASPState] GO /****** Object: StoredProcedure [dbo].[TempGetAppID] Script Date: 11/21/2020 10:19:18 ******/ /* Session 共享方案 在IIS中找到站点的ID 然后在 tempdb.dbo.ASPStateTempApplications 表中找到站点的 appName 以及它的父级站点 将查询到的 appName 赋值给下面的 @appPMame 这样就可以在这个站点下的所有 子应用都共享Session */ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO ALTER PROCEDURE [dbo].[TempGetAppID] @appName tAppName, @appId int OUTPUT AS declare @appPMame varchar(100)='/lm/w3svc/{IIS站点ID}/root' --站点父级APPName SET @appName = LOWER(@appName) SET @appId = NULL --if @appName='/lm/w3svc/90/root/name' --具体子应用程序共享 if CHARINDEX(@appPMame, @appName )>0 --所有子应用程序共享 begin select @appId= appid FROM [tempdb].dbo.ASPStateTempApplications where AppName=@appPMame end if @appId is null begin SELECT top 1 @appId = AppId FROM [tempdb].dbo.ASPStateTempApplications WHERE AppName = @appName end IF @appId IS NULL BEGIN BEGIN TRAN SELECT top 1 @appId = AppId FROM [tempdb].dbo.ASPStateTempApplications WITH (TABLOCKX) WHERE AppName = @appName IF @appId IS NULL BEGIN EXEC GetHashCode @appName, @appId OUTPUT INSERT [tempdb].dbo.ASPStateTempApplications VALUES (@appId, @appName) IF @@ERROR = 2627 BEGIN DECLARE @dupApp tAppName SELECT @dupApp = RTRIM(AppName) FROM [tempdb].dbo.ASPStateTempApplications WHERE AppId = @appId RAISERROR('SQL session state fatal error: hash-code collision between applications ''%s'' and ''%s''. Please rename the 1st application to resolve the problem.', 18, 1, @appName, @dupApp) END END COMMIT END RETURN 0
执行成功后,建议重启数据库与IIS,清空一下Session
declare @appid int declare @appname varchar(100)='/lm/w3svc/20/root/pdf' exec [TempGetAppID] @appname=@appname,@appid=@appid output select @appid
执行验证一下结果