servicestack,Session,Authentication,servicestack" /> servicestack,Session,Authentication,servicestack" />

Session Servicestack-在会话之间传递信息

Session Servicestack-在会话之间传递信息,session,authentication,servicestack,Session,Authentication,servicestack,我已经实现了自定义AuthenticateAttribute、AuthUserSession和CredentialAuthProvider。在AuthenticateAttribute的Execute方法中,我执行以下操作: public override void Execute(IRequest request, IResponse response, object requestDto) { var session = request.GetSession() as

我已经实现了自定义AuthenticateAttribute、AuthUserSession和CredentialAuthProvider。在AuthenticateAttribute的Execute方法中,我执行以下操作:

public override void Execute(IRequest request, IResponse response, object requestDto)
    {
        var session = request.GetSession() as IMyCustomAuthUserSession;

        // Copy certain request headers into a dictionary on my session object
    }
我需要存储某些发送给我供以后使用的特殊标题。当未启用身份验证时,此功能正常工作。启用身份验证且用户必须登录时,my CredentialAuthProvider类的TryAuthenticate方法将激发:

    public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
    {
        var session = authService.GetSession() as IMyCustomAuthUserSession;
    }
这些to方法中的会话不同,因为我在TryAuthenticate方法中获得的会话(在AuthenticateAttribute.Execute方法之后激发)不包含我存储在其中的头

特殊头只在第一次调用web服务器时发送,因此我需要将它们放入TryAuthenticate方法的新会话中


如何才能做到这一点?

在登录之间传递会话数据将很困难,因为在身份验证尝试之间会话将失效。通过配置
AuthFeature
插件,您可以选择在登录之间保留相同的会话cookie:

Plugins.Add(new AuthFeature(...) {
    GenerateNewSessionCookiesOnAuthentication = false
});
登录时将保留相同的用户
ss id/ss pid
cookies

使用SessionBag在Auth SessionNos之间持久化数据 要在经过身份验证的用户会话之外持久化数据,您可以,例如:

然后,在后续请求中,您可以使用唯一的Cookie Id保存数据,例如:

var uniqueId = req.GetSessionParam("my-id");
var cacheKey = $"urn:Cart:{uniqueId}";
var cache = req.GetCacheClient();
cache.Set(cacheKey, new Cart { ... });
然后,稍后使用以下命令检索它:

var uniqueId = req.GetSessionParam("my-id");
var cacheKey = $"urn:Cart:{uniqueId}";
var cache = req.GetCacheClient();
var cart cache.Get<Cart>(cacheKey);
var uniqueId=req.GetSessionParam(“我的id”);
var cacheKey=$“urn:Cart:{uniqueId}”;
var cache=req.GetCacheClient();
var cart cache.Get(cacheKey);
var uniqueId = req.GetSessionParam("my-id");
var cacheKey = $"urn:Cart:{uniqueId}";
var cache = req.GetCacheClient();
cache.Set(cacheKey, new Cart { ... });
var uniqueId = req.GetSessionParam("my-id");
var cacheKey = $"urn:Cart:{uniqueId}";
var cache = req.GetCacheClient();
var cart cache.Get<Cart>(cacheKey);