SignalR Context.User与ApplicationCookie一起为空

LRF*_*k01 4 signalr owin asp.net-identity

Context.User在我的集线器中为空,我不知道为什么.

的Starup:

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            AuthenticationMode = AuthenticationMode.Passive,
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity =
                    SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(5),
                        regenerateIdentity:
                            (manager, user) =>
                                user.GenerateUserIdentityAsync(manager, DefaultAuthenticationTypes.ApplicationCookie))
            }
        });
        app.MapSignalR();

        ConfigureWebApi(app);

        app.UseNancy();
Run Code Online (Sandbox Code Playgroud)

毂:

public class TestHub : Hub
{
    public void Hello()
    {
        Clients.All.hello(DateTime.Now.ToString("F"));
    }
    public void CurrentUser()
    {
        var user = Context.User;
        Clients.Caller.currentUser(user.Identity);
    }
}
Run Code Online (Sandbox Code Playgroud)

CurrentUser方法抛出异常,因为Context.User为null.我不确定哪些额外信息会有所帮助.我想我可以从当前的owin语境中得到这个,但是我没有办法得到这个.我试图创建一个IAuthorizeHubConnection属性,但我找不到从IRquest对象获取当前owin上下文的方法.我可以做一个新的.

Kév*_*let 5

SignalR目前无法使用配置为被动模式的身份验证中间件触发按需身份验证(例如Web/WWO可以使用其HostAuthenticationFilter/Attribute).切换到活动模式,它应该工作:

app.UseCookieAuthentication(new CookieAuthenticationOptions {
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    AuthenticationMode = AuthenticationMode.Active,
    Provider = new CookieAuthenticationProvider {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(5),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager, DefaultAuthenticationTypes.ApplicationCookie)) }
});
Run Code Online (Sandbox Code Playgroud)