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();
集线器:
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);
}
}
CurrentUser方法抛出异常,因为Context.User为null。我不确定哪些额外信息会有所帮助。我想我可以从当前的owin语境中得到这个,但是我没有办法得到这个。我试图制作一个IAuthorizeHubConnection属性,但我找不到从IRquest对象获取当前owin上下文的方法。我可以做一个新的。
答案 0 :(得分:5)
SignalR目前无法使用配置为被动模式的身份验证中间件触发按需身份验证(例如Web OS w / OWIN可以使用其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)) }
});