How to get the session details in servicestack application from SQLServer using session id sent from mvc application?

廉价感情. 提交于 2019-12-24 12:24:21

问题


I have two applications deployed on different servers.

1) ASP.net MVC 4 web application with sessionState mode="SQLServer" for session management

2) Servicestack service application with sessionState mode="SQLServer" for session management ( yes i need session in service application )

both application share same database and tables for session management

I am using following solution to have asp.net session in service stack application .

Reconnecting to Servicestack session in an asp.net MVC4 application

I am not using the servicestack session so following way is not available to me.

How can I use a standard ASP.NET session object within ServiceStack service implementation

User logs in to the mvc web application, for user data mvc application in turn calls the service methods from service stack application.

using (var client = new JsonServiceClient(servicestackUrl))
                {

                    var response = client.Get<HttpWebResponse>(new GetFile
                    {
                        UserName = UserName,
                        EmployerID = EmployerID,
                        filename = fileName

                    });

                    //work on responce
                }

now i require to have same session variables initialized in mvc application in Servicestack application deployed on another server

session cookie is set in mvc web application not on servicestack application ,but session details are stored in sql database accessible to both application.

I can get the current session id and send it to service as parameter , but how to get the session details from database using session id ?.

Update :

I have found a way to send session id to app server

 var cookie = new Cookie("ASP.NET_SessionId", Session.SessionID);

 cookie.Domain = ".app.localhost";

 client.CookieContainer.Add(cookie);

How to get session in serviceStack application using above from sql server ?


回答1:


ServiceStack doesn't use ASP.NET's Session Provider for storing Sessions, it uses its own Session API which stores Sessions into the registered Caching provider.

E.g. to have Sessions stored in SQL Server you would use OrmLiteCacheClient which can be registered with:

//Register OrmLite Db Factory if not already
container.Register<IDbConnectionFactory>(c => 
    new OrmLiteConnectionFactory(connString, SqlServerDialect.Provider)); 

container.RegisterAs<OrmLiteCacheClient, ICacheClient>();

//Create 'CacheEntry' RDBMS table if it doesn't exist already
container.Resolve<ICacheClient>().InitSchema(); 

To access the Session you'd need to access the Session Id (i.e. ss-id or ss-pid Cookies) which you can get from an IRequest with:

var sessionId = req.GetSessionId();

Then you can use SessionFeature.GetSessionKey() to access the cache key that's used to store the Session, i.e:

var sessionKey = SessionFeature.GetSessionKey(sessionId);

Which you can then access from the ICacheClient with:

var userSession = cache.Get<IAuthSession>(sessionKey);

Note the easiest way to access the Session for the current user is with just:

var userSession = req.GetSession();

Accessing ASP.NET Sessions within ServiceStack

Since ServiceStack is just a normal ASP.NET Web Application you can continue to access the ASP.NET Request Context via the HttpContext.Current singleton, e.g. you can access an ASP.NET Session with:

var value = HttpContext.Current.Session[key];

Also as a ServiceStack Request is just an ASP.NET IHttpHandler underneath, you can also access the underlyng ASP.NET HttpRequest with:

var aspReq = req.OriginalRequest as HttpRequestBase;
var value = aspReq.RequestContext.HttpContext.Session[key];

Note: ServiceStack has its own clean Session and Caching Providers which is completely independent from ASP.NET Session or Caching Provider model which has degraded performance by design.



来源:https://stackoverflow.com/questions/31917908/how-to-get-the-session-details-in-servicestack-application-from-sqlserver-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!