AspNetSessionState icon indicating copy to clipboard operation
AspNetSessionState copied to clipboard

SqlSessionStateProviderAsync Session Id changes on App Service Slot Swap (Everyone gets logged out)

Open robs opened this issue 2 years ago • 0 comments

I'm raising this because I spent hours and hours down Machine Key related rabbit holes and am hoping to save anyone else the joy of that :)

The long and short of it is that:

  1. AppId isn't settable via any config option, it only ever uses the static HttpRuntime.AppDomainAppId
  2. GetHashCode doesn't always return the same value for a given string so even if you set HttpRuntime.AppDomainAppId via reflection, the value from GetHashCode() still changes.

This is where the session id is suffixed with HttpRuntime.AppDomainAppId.GetHashCode().ToString("X8", CultureInfo.InvariantCulture) https://github.com/aspnet/AspNetSessionState/blob/06de5f5270c6d9b7d7f6bd1a0bc6343b8c0a56f5/src/SqlSessionStateProviderAsync/SqlSessionStateProviderAsync.cs#L128C77-L128C77

I'm not sure what the proper resolution is but setting HttpRuntime.AppDomainAppId (via reflection) at start up and just appending the AppId string without the call to GetHashCode.ToString(X8) means that the session id doesn't change across slot swaps.

Happy to provide any other info if needed.

robs avatar Jul 21 '23 16:07 robs