gpt4 book ai didi

SignalR Context.User 与 ApplicationCookie 为空

转载 作者:行者123 更新时间:2023-12-05 01:03:51 25 4
gpt4 key购买 nike

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

启动:

        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 上下文的方法。我只能做一个新的。

最佳答案

SignalR 目前无法使用配置为被动模式的身份验证中间件来触发按需身份验证(如带有 OWIN 的 Web API 可以使用其 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)) }
});

关于SignalR Context.User 与 ApplicationCookie 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24175799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com