ASP.NET implements the method of sharing Session value between multiple domain names and multiple websites

  • 2020-05-16 06:47:03
  • OfStack

ASP.NET enables multiple domain names and multiple websites to share Session values
1. Implementation: you can set which sites can share the Session value, so as to prevent others from using it to visit
To do this, you have to put the Session values into the database, so register one with the VS command tool
The name is as follows: aspnet_regsql.exe-S [database service address] -E-ssadd
After the successful addition, we need to make some simple changes to the stored procedure generated by ASP.NET,
Open ASP.net and find the stored procedure "TempGetAppID" for the database "ASPState" we created
Then modify the stored procedure as follows:
 
USE [ASPState] 
GO 
/****** Object: StoredProcedure [dbo].[TempGetAppID] Script Date: 11/21/2011 16:15:27 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
ALTER PROCEDURE [dbo].[TempGetAppID] 
@appName tAppName, 
@appId int OUTPUT 
AS 
SET @appName = LOWER(@appName) 
SET @appId = NULL 
set @appId=640732509 -- I wrote this on my own test   You can go through the system database tempdb In the ASPStateTempApplications Query to site appId  Remember only when the site is Seeion The configuration is in the database   And it was saved 1 time Session The value can only be queried  
/* 
--SELECT @appId = AppId 
--FROM [tempdb].dbo.ASPStateTempApplications 
--WHERE AppName = @appName 
 The above annotation method is original ASP.net  autogenerated  
*/ 
IF @appId IS NULL BEGIN 
BEGIN TRAN 
SELECT @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 

We can share this, but one of the things we have to do is replace the SeeionID client, we can add a handler to take advantage of the SessionIDManager class,
I'm not going to write the example, I'm going to write the substitution method
 
SessionIDManager sessionid = new SessionIDManager(); 
bool a; 
bool b; 
sessionid.SaveSessionID(this.Context, " Here is the SeeionID", out a, out b); 

Finally, modify the configuration file
Add under the System.Web node < sessionState mode="SQLServer" sqlConnectionString="data source= data connection; user id = sa; password = 123456;" > < /sessionState >
ok 1 cut it and you try it

Related articles: