我在我的一个项目中使用ServiceStack作为基础库。 我已经在两个部分API和WEB应用程序中构建了我的应用程序,它们是独立的项目和存储库。负载平衡环境中的ServiceStack会话处理
身份验证应该发生在API层,它应该被缓存在那里。我在API服务器中使用Ormlite缓存客户端。
在API AppHost.cs
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())
}));
在APICredentialsAuthProvider我现在的储蓄它获取存储在分贝CacheEntry
表
我使用apiurl/auth
通过Ajax调用验证在Web应用程序的用户会话其返回AuthenticateResponse
与sessionId
与它。
我正在将此sessionId更新为cookie,作为s-id
,稍后在预先请求过滤器中根据请求类型将其更新为ss-id
或ss-pid
。
//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)
});
这种方法不获取从缓存中的会话是Ormlite在我的情况,并在网站和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应用程序(每个请求)。
是否有任何替代解决方案可以实现相同?
在此先感谢!