Paolo ponzano - 34 - Nov 17, 2014

Hello Demis,
I’ve encountered a strange problem regarding the sessions… We use a custom auth provider that lays on SqlServer’s session… it’s registered at this way

public override void Configure(Funq.Container container)
{
    container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings[“default”].ConnectionString, SqlServerDialect.Provider));
    container.RegisterAs<OrmLiteCacheClient, ICacheClient>();
    container.Resolve<ICacheClient>().InitSchema();

    Plugins.Add(
        new IdeaAuthFeature(
            ConfigurationManager.ConnectionStrings[“idea”].ConnectionString,
            appSettings.Get(“auth.slidingExpiration”, new TimeSpan(0,20,0))
        )
    );
}

The source code of the provider is shared here https://drive.google.com/file/d/0B6nPFuIuKHG1cDdfZ3pfRGpxMjg/view?usp=sharing

Servicestack had created the CacheEntry table correctly and profiling the DB I’ve found that a sesson is beign ovewritten wrongly…

2014-11-13 12:33:15.360        exec sp_executesql N’INSERT INTO “CacheEntry” (“Id”,“Data”,“ExpiryDate”,“CreatedDate”,“ModifiedDate”) VALUES (@Id,@Data,@ExpiryDate,@CreatedDate,@ModifiedDate)’,N’@Id nvarchar(37),@Data nvarchar(1146),@ExpiryDate datetime,@CreatedDate datetime,@ModifiedDate datetime’,@Id=N’urn:iauthsession:XgR7XAwsHwPmt35HIL8W’,@Data=N’{__type:“IF.Auth.IdeaAuthUserSession, IF.Core”,UserId:98403,InstituteId:84,Ndg:000000001       ,ExtraParameters:[],Id:XgR7XAwsHwPmt35HIL8W,UserAuthName:CC00145,UserName:CC00145,DisplayName:FRANCO NERI,CreatedAt:2014-11-13T11:33:16.1113157Z,LastModified:2014-11-13T11:33:16.1113157Z,ProviderOAuthAccess:[],Roles:[Administrator,Reader],Permissions:[],IsAuthenticated:True,Tag:0}’,@ExpiryDate=‘2014-11-13 13:33:16.110’,@CreatedDate=‘2014-11-13 11:33:16.110’,@ModifiedDate=‘2014-11-13 11:33:16.110’

2014-11-13 12:33:15.363        UPDATE “CacheEntry” SET “Data”=’{__type:“IF.Auth.IdeaAuthUserSession, IF.Core”,UserId:98403,InstituteId:84,Ndg:000000001       ,ExtraParameters:[],Id:XgR7XAwsHwPmt35HIL8W,UserAuthName:CC00145,UserName:CC00145,DisplayName:FRANCO NERI,CreatedAt:2014-11-13T11:33:16.1113157Z,LastModified:2014-11-13T11:33:16.1113157Z,ProviderOAuthAccess:[],Roles:[Administrator,Reader],Permissions:[],IsAuthenticated:True,Tag:0}’, “ExpiryDate”=‘20141113 13:33:16.111’, “ModifiedDate”=‘20141113 11:33:16.111’ WHERE (“Id” = ‘urn:iauthsession:XgR7XAwsHwPmt35HIL8W’)

2014-11-13 12:33:51.680        UPDATE “CacheEntry” SET “Data”=’{__type:“IF.Auth.IdeaAuthUserSession, IF.Core”,UserId:98403,InstituteId:84,Ndg:000000001       ,ExtraParameters:[],Id:XgR7XAwsHwPmt35HIL8W,UserAuthName:CC00145,UserName:CC00145,DisplayName:FRANCO NERI,CreatedAt:2014-11-13T12:33:16.1113157+01:00,LastModified:2014-11-13T12:33:16.1113157+01:00,ProviderOAuthAccess:[],Roles:[Administrator,Reader],Permissions:[],IsAuthenticated:True,Tag:0}’, “ExpiryDate”=‘20141113 13:33:52.430’, “ModifiedDate”=‘20141113 11:33:52.430’ WHERE (“Id” = ‘urn:iauthsession:XgR7XAwsHwPmt35HIL8W’)

2014-11-13 12:35:19.767        UPDATE “CacheEntry” SET “Data”=’{__type:“IF.Auth.IdeaAuthUserSession, IF.Core”,UserId:98403,InstituteId:84,Ndg:000000001       ,ExtraParameters:[],Id:XgR7XAwsHwPmt35HIL8W,UserAuthName:CC00145,UserName:CC00145,DisplayName:FRANCO NERI,CreatedAt:2014-11-13T12:33:16.1113157+01:00,LastModified:2014-11-13T12:33:16.1113157+01:00,ProviderOAuthAccess:[],Roles:[Administrator,Reader],Permissions:[],IsAuthenticated:True,Tag:0}’, “ExpiryDate”=‘20141113 13:35:20.530’, “ModifiedDate”=‘20141113 11:35:20.530’ WHERE (“Id” = ‘urn:iauthsession:XgR7XAwsHwPmt35HIL8W’)

2014-11-13 12:41:57.773        UPDATE “CacheEntry” SET “Data”=’{__type:“IF.Auth.IdeaAuthUserSession, IF.Core”,UserId:142524,InstituteId:84,Ndg:000000002       ,ExtraParameters:[],Id:npK6nhS3H78b8PDMO0DR,UserAuthName:ccp0587,UserName:ccp0587,DisplayName:MARIO ROSSI,CreatedAt:2014-11-13T11:41:58.5367722Z,LastModified:2014-11-13T11:41:58.5367722Z,ProviderOAuthAccess:[],Roles:[Administrator,Reader],Permissions:[],IsAuthenticated:True,Tag:0}’, “ExpiryDate”=‘20141113 13:41:58.552’, “ModifiedDate”=‘20141113 11:41:58.552’ WHERE (“Id” = ‘urn:iauthsession:npK6nhS3H78b8PDMO0DR’)

2014-11-13 12:41:57.773        exec sp_executesql N’INSERT INTO “CacheEntry” (“Id”,“Data”,“ExpiryDate”,“CreatedDate”,“ModifiedDate”) VALUES (@Id,@Data,@ExpiryDate,@CreatedDate,@ModifiedDate)’,N’@Id nvarchar(37),@Data nvarchar(1146),@ExpiryDate datetime,@CreatedDate datetime,@ModifiedDate datetime’,@Id=N’urn:iauthsession:npK6nhS3H78b8PDMO0DR’,@Data=N’{__type:“IF.Auth.IdeaAuthUserSession, IF.Core”,UserId:142524,InstituteId:84,Ndg:000000002       ,ExtraParameters:[],Id:npK6nhS3H78b8PDMO0DR,UserAuthName:ccp0587,UserName:ccp0587,DisplayName:MARIO ROSSI,CreatedAt:2014-11-13T11:41:58.5367722Z,LastModified:2014-11-13T11:41:58.5367722Z,ProviderOAuthAccess:[],Roles:[Administrator,Reader],Permissions:[],IsAuthenticated:True,Tag:0}’,@ExpiryDate=‘2014-11-13 13:41:58.553’,@CreatedDate=‘2014-11-13 11:41:58.553’,@ModifiedDate=‘2014-11-13 11:41:58.553’

2014-11-13 12:43:52.397        UPDATE “CacheEntry” SET “Data”=’{__type:“IF.Auth.IdeaAuthUserSession, IF.Core”,UserId:98403,InstituteId:84,Ndg:000000001       ,ExtraParameters:[], Id:XgR7XAwsHwPmt35HIL8W,UserAuthName:CC00145,UserName:CC00145,DisplayName:FRANCO NERI,CreatedAt:2014-11-13T12:33:16.1113157+01:00,LastModified:2014-11-13T12:33:16.1113157+01:00,ProviderOAuthAccess:[],Roles:[Administrator,Reader],Permissions:[],IsAuthenticated:True,Tag:0}’, “ExpiryDate”=‘20141113 13:43:53.161’, “ModifiedDate”=‘20141113 11:43:53.161’ WHERE (“Id” = ‘urn:iauthsession:npK6nhS3H78b8PDMO0DR’)

As you can see at

At 2014-11-13 12:33:15.360  the session of FRANCO NERI is inserted with Id = XgR7XAwsHwPmt35HIL8W.
At 2014-11-13 12:41:57.773 the session of MARIO ROSSI is inserted with Id = npK6nhS3H78b8PDMO0DR.

Updates are correct except the one  where the session “npK6nhS3H78b8PDMO0DR” (Mario Rossi)  is overwritted with the data of Franco Neri…

have you got any idea of why this happens?

Thanks

Nope, I can’t make sense of it, and the source code you’ve given me isn’t enough to reproduce it so I can only go off these 3 source files. What I don’t understand is if you’re using MemoryCacheClient and InMemoryAuthRepository (in your IdeaAuthFeature) why the cache is being saved to the database? On a side note I personally would be passing the same IDbConnectionFactory dependency into your IdeaAuthProvider instead of setting a connection string.

But I can’t tell why it’s using a different session id, it would be more useful if you could also log the HTTP Request Headers to see if the session id used matches up with the request. 

paolo ponzano:

Hello Demis,
I’ve done some testing and I’ve removed the Sliding Expiration since it was one of the 2 points where the session was manipulated… today I got the same problem as the other day… the strange thing is that the same iauthsession for 2 different clients… not it seems that the same SQL connection has been reused by the ServiceStack factory… is this a possible issue? 
Thanks

The same physical db connection can be reused when using connection pooling but this should be a transparent impl detail.

In addition to db logging can you log an instance of the session (which you can do with T.Dump() / T.PrintDump() ext methods) together with the SessionId (which you can get with IRequest.GetSessionId() ext method).