心跳期间的ServiceStack GetSession

时间:2016-03-03 10:06:53

标签: c# .net servicestack httpserver

我有一个servicestack应用程序,我想在心跳期间进行一些与会话相关的更新 我简化的主机代码:

internal class SelfHost : AppSelfHostBase
{
    public SelfHost() : base("HttpListener Self-Host", typeof(ServerEventsService).Assembly)
    {
    }

    public override void Configure(Container container)
    {
        Plugins.Add(new ServerEventsFeature
        {
            LimitToAuthenticatedUsers = true,
            NotifyChannelOfSubscriptions = false,
            OnHeartbeatInit = req =>
            {
                var userSession = req.GetSession();
                var sessionKey = SessionFeature.GetSessionKey(req.GetSessionId());
            }
        });

        container.Register<ICacheClient>(new MemoryCacheClient());

        Plugins.Add(new AuthFeature(() => new AuthUserSession(),
            new IAuthProvider[]
            {
                new CredentialsAuthProvider
                {
                    SessionExpiry = TimeSpan.FromSeconds(30),
                    SkipPasswordVerificationForInProcessRequests = true,
                },
            })
            );

        var userRep = new InMemoryAuthRepository();
        container.Register<IUserAuthRepository>(userRep);

        string hash;
        string salt;
        var pwd = "ValidPassword";

        new SaltedHash().GetHashAndSaltString(pwd, out hash, out salt);
        userRep.CreateUserAuth(new UserAuth
        {
            Id = 3,
            DisplayName = "CustomDisplayName2",
            Email = "test2@gmail.com",
            UserName = "CustomUserName2",
            FirstName = "FirstName2",
            LastName = "LastName2",
            PasswordHash = hash,
            Salt = salt,
        }, pwd);
    }
}

和简化的客户端代码:

var mainClient = new ServerEventsClient(baseUrl, "home");

var authResponse = mainClient.Authenticate(new Authenticate
{
    provider = "credentials",
    UserName = "CustomUserName2",
    Password = "ValidPassword",
    RememberMe = false,
});

mainClient.Connect().Wait();

它连接并验证正常,有问题的部分从OnHeartbeatInit开始 - 我的会话总是一个新的非经过身份验证的会话,只有Id,CreatedAt和LastModified。我的服务在类级别上标有[Authenticate]属性。我是否错误配置或丢失了应该允许的配置?如果我发布到我的服务并致电Request.GetSession(),那么我会收到一个我希望在OnHeartbeatInit中看到的填充会话。
旁注:在任何地方都没有隐藏的配置,我专门为这个例子创建了一个单独的应用程序。

1 个答案:

答案 0 :(得分:2)

问题是,在.NET ServerEventsClient中发送的检测信号请求不会与客户端验证或连接到CookieContainer的{​​{1}}一起发送,这是包含会话Cookie的内容。

我已更新/event-stream,因此心跳请求现在为每个心跳请求引用相同的CookieContainer in this commit

此更改现在可从v4.0.55获得,现在为available on MyGet