Session 负载平衡环境中的ServiceStack会话处理
我在我的一个项目中使用ServiceStack作为基本库。 我用API和WEB应用程序两部分构建了我的应用程序,这两部分是独立的项目和存储库 身份验证应该发生在API层上,并且应该缓存在那里。我正在API服务器中使用Ormlite缓存客户端 在API AppHost.cs中Session 负载平衡环境中的ServiceStack会话处理,session,servicestack,Session,servicestack,我在我的一个项目中使用ServiceStack作为基本库。 我用API和WEB应用程序两部分构建了我的应用程序,这两部分是独立的项目和存储库 身份验证应该发生在API层上,并且应该缓存在那里。我正在API服务器中使用Ormlite缓存客户端 在API AppHost.cs中 var dbFactory = new OrmLiteConnectionFactory("ConnectionString",SqlServerDialect.Provider); container.RegisterAs
var dbFactory = new OrmLiteConnectionFactory("ConnectionString",SqlServerDialect.Provider);
container.RegisterAs<OrmLiteCacheClient, ICacheClient>();
container.Resolve<ICacheClient>().InitSchema();
Plugins.Add(new AuthFeature(() => new APISession(),
new IAuthProvider[] {
new APICredentialsAuthProvider(new AppSettings())
}));
这种方法不会从缓存中获取会话,在我的例子中,缓存是Ormlite,在Web和Api应用程序中提供了相应的配置
实现这一目标的最佳方法是什么?
但是,我可以使用缓存客户端访问会话
//Inside Web application apphost
this.PreRequestFilters.Add((req, res) =>
{
System.Net.Cookie cookie = req.Cookies["s-id"];
req.Cookies["ss-id"] = cookie;
req.SetSessionId(cookie.Value);
APISession cachedSession = GetCacheClient(req).Get<APISession(SessionFeature.GetSessionKey(cookie.Value));
WEBSession session.PopulateWith<WEBSession, APISession>(cachedSession);
});
//Web应用程序apphost内部
this.PreRequestFilters.Add((请求,请求)=>
{
System.Net.Cookie Cookie=请求Cookies[“s-id”];
请求Cookies[“ss id”]=cookie;
请求设置会话ID(cookie.Value);
APISession cachedSession=GetCacheClient(req)。Get如果您在同一域后对多个Web应用进行负载平衡,则在向以下任一应用发出请求时,使用类似的或mliteCacheClient
将发送ServiceStack的ss id/ss pid
Cookies:
http://example.org/app1 -> http://internal:8001/
/app2 -> http://internal:8002/
/app3 -> http://internal:8003/
然后,只要每个应用程序都配置了相同的或mlitecacheclient
,在一个应用程序中创建的会话就会在所有3个应用程序中可见
通过将会话设置为IRequest.Items
,您可以阻止进一步的DB访问以检索该请求的会话,例如:
req.Items[Keywords.Session] = session;
然后,针对该请求对会话的任何访问都将从IRequest
Items字典中解析,而不是访问数据库
另一种可供选择的身份验证解决方案是使用无状态身份验证
req.Items[Keywords.Session] = session;