如何利用MVC应用程序发送会话ID从SQLServer的获得servicestack应用程序的会话信息? [英] How to get the session details in servicestack application from SQLServer using session id sent from mvc application?

查看:275
本文介绍了如何利用MVC应用程序发送会话ID从SQLServer的获得servicestack应用程序的会话信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经部署在不同服务器上的两个应用程序。

I have two applications deployed on different servers.

1)ASP.net MVC使用的sessionState模式=SQLServer的4 Web应用程序会话管理

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

2)的sessionState模式=SQLServer的会话管理Servicestack服务应用程序(是的,我需要的服务应用程序会话)

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

我是用以下解决方案来在服务栈的应用asp.net会话。

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

<一个href=\"http://stackoverflow.com/questions/19160839/reconnecting-to-servicestack-session-in-an-asp-net-mvc4-application\">Reconnecting在一个asp.net应用MVC4 Servicestack会议

Reconnecting to Servicestack session in an asp.net MVC4 application

我不使用的servicestack会话,这样下面的方式并不适用于我。

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

<一个href=\"http://stackoverflow.com/questions/8339584/how-can-i-use-a-standard-asp-net-session-object-within-servicestack-service-impl\">How我可以用ServiceStack服务实现中的一个标准的ASP.NET会话对象

到MVC Web应用程序,用户数据用户登录反过来MVC应用程序调用从服务栈的应用服务方法。

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
                }

现在我需要有在Servicestack应用在MVC应用程序相同的初始化会话变量 部署另一台服务器上

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

会话cookie设置在不servicestack应用MVC Web应用程序,但是会议详细信息存储在这两个应用程序访问SQL数据库。

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

我可以得到当前的会话ID,并将其发送到服务的参数,但如何使用会话ID,摆脱数据库中的会话信息?

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 ?.

更新:

我已经找到一种方法,以会话ID发送到应用服务器

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);

如何serviceStack应用上面使用从SQL Server获取会话?

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

推荐答案

ServiceStack不使用ASP.NET的存储时间Session提供商,它使用自己的会话API存储会话到注册的https://github.com/ServiceStack/ServiceStack/wiki/Caching相对=nofollow >缓存提供商

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.

例如。已经存储在SQL Server会话你可以使用 OrmLiteCacheClient 可与注册:

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(); 

要访问会话你需要访问<一个href=\"https://github.com/ServiceStack/ServiceStack/wiki/Sessions#permanent-and-temporary-session-ids\"相对=nofollow>会话ID (即SS-ID或SS-PID饼干),你可以从获得 IRequest

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();

然后你可以使用 SessionFeature.GetSessionKey()来访问的用于存储会话,缓存键即:

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);

然后你可以从访问 ICacheClient

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();

内ServiceStack

访问ASP.NET会话

由于ServiceStack仅仅是一个正常的ASP.NET Web应用程序,您可以继续通过 HttpContext.Current 单,例如访问ASP.NET请求上下文您可以访问ASP.NET会话:

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];

另外,作为一个ServiceStack请求仅仅是一个ASP.NET 的IHttpHandler 底下,你也可以<一个href=\"https://github.com/ServiceStack/ServiceStack/wiki/Access-HTTP-specific-features-in-services#accessing-the-ihtt$p$pquest-and-ihtt$p$psponse-in-filters-and-services\"相对=nofollow>访问underlyng ASP.NET的Htt prequest 有:

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

请注意:ServiceStack都有自己干净对话和的缓存提供商这是从具有的设计的性能下降。

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.

这篇关于如何利用MVC应用程序发送会话ID从SQLServer的获得servicestack应用程序的会话信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆