I am using SQL Server 2008 to store the sessions for my .NET4 application. The sessions will be stored in [DatabaseA]. There isn't any other custom configuration for this, so the ASPState database is exactly how it would come out of the box (using aspnet_regsql)
My main application runs on [DatabaseB] (same server). Within this database I have a 2 tables that record a some data along with the sessionID.
When the [DeleteExpiredSessions] stored procedure (on DatabaseA) is run via SQL Agent, the sessions are correctly removed from the ASPState, but I want to extend this to delete the rows based on the SessionID from [DatabaseB]
I have tried editing the [DeleteExpiredSessions] stored procedure to include the following SQL
OPEN ExpiredSessionCursor
FETCH NEXT FROM ExpiredSessionCursor INTO @SessionID
WHILE @@FETCH_STATUS = 0
BEGIN
-- BEGIN MY ADDITIONS
DECLARE @myRecordCount int
SELECT @myRecordCount= COUNT(*) FROM [DatabaseB].dbo.Table1 WHERE [DatabaseB].dbo.Table1.SessionId = @SessionID -- AND [DatabaseB].dbo.Table1.DateEntered < @now
SELECT @myRecordCount
DELETE FROM [DatabaseB].dbo.Table1 WHERE [DatabaseB].dbo.Table1.SessionId = @SessionID AND [DatabaseB].dbo.Table1.DateEntered < @now
DELETE FROM [DatabaseB].dbo.Table2 WHERE [DatabaseB].dbo.Table2.SessionId = @SessionID AND [DatabaseB].dbo.Table2.DateEntered < @now
-- END MY ADDITIONS
DELETE FROM [DatabaseA].dbo.ASPStateTempSessions WHERE SessionID = @SessionID AND Expires < @now
FETCH NEXT FROM ExpiredSessionCursor INTO @SessionID
END
CLOSE ExpiredSessionCursor
DEALLOCATE ExpiredSessionCursor
But @myRecordCount is returning 0 rows. There are no errors reported (the agent job runs correctly, and nothing in SQL Profiler), and @myRecordCount should be returning 4 in this instance.
The DECLARE/SELECT COUNT is there as a debugger.
UPDATE
so having debugged the sql and found that :
SELECT SessionId
FROM [ASPState].dbo.ASPStateTempSessions WITH (READUNCOMMITTED)
WHERE Expires < GETUTCDATE()
--Returns SessionId = '3wj5nyrlz02ezw1vvjts4gjv28d8c075'
SELECT * FROM [DatabaseB].dbo.Table1 -- returns (4) results
SELECT * FROM [DatabaseB].dbo.Table1 WHERE [DatabaseB].dbo.Table1.SessionId = '3wj5nyrlz02ezw1vvjts4gjv28d8c075' -- returns (0) results
I have deduced that the SessionId is wrong.
What is being stored in [DatabaseB].dbo.Table1
is '3wj5nyrlz02ezw1vvjts4gjv' - notice the truncation of the string.
Now my .NET code (using EF6.1.3) for storing the session variables are
Table1.SessionId = HttpContext.Current.Session.SessionID
this means that 3wj5nyrlz02ezw1vvjts4gjv
is being stored.
The Cookie ASP.NET_SessionId
also has the same value.
The length of the SessionId column of Table1 is the same as the ASPState tmpSession table (88)
Updated Question
Interestingly the ASPStateTempSessions.SessionId variable seems to be appending28d8c075
to the end of it.
So ASP.NET_SessionId and HttpContext.Current.Session.SessionID are 3wj5nyrlz02ezw1vvjts4gjv
but ASPStateTempSessions.SessionId is 3wj5nyrlz02ezw1vvjts4gjv28d8c075
I have just cleared all session variables and cookies and I have a new session variable, but the 28d8c075 is still being removed/appended to the various cookies and data values.
I understand this is happening because the ASPStateTempSessions is appending a suffix of the application hash to the SessionId (https://msdn.microsoft.com/en-us/library/aa478952.aspx)
Column Name Column Type Description
SessionId nvarchar(88) Session ID + application ID
How do I return this to HttpContext.Current.Session.SessionID instead of just the SessionId?
AppId
can be in HEX inSessionId
field? – KiquenetSUBSTRING(a.SessionId, 25, 8) AS AppIDHex
and convert AppId to HEXSUBSTRING(sys.fn_varbintohexstr(CONVERT(VarBinary,b.AppId)), 3, 8)
– Kiquenet