C# SignenManager IsSignedIn在信号器集线器中返回false
我部署了Asp.Net Core应用程序,该应用程序具有SignalR功能(Gray.microsoft.aspnetcore.SignalR.server包)和whant,以在Hub类中添加身份授权检查C# SignenManager IsSignedIn在信号器集线器中返回false,c#,asp.net-core,signalr,asp.net-identity,C#,Asp.net Core,Signalr,Asp.net Identity,我部署了Asp.Net Core应用程序,该应用程序具有SignalR功能(Gray.microsoft.aspnetcore.SignalR.server包)和whant,以在Hub类中添加身份授权检查 public class FooHub : Hub { private SignInManager<ApplicationUser> SignInManager; public FooHub(SignInManager<ApplicationUser>
public class FooHub : Hub
{
private SignInManager<ApplicationUser> SignInManager;
public FooHub(SignInManager<ApplicationUser> SignInManager)
{
this.SignInManager = SignInManager;
}
//server method with authorization checking
public void Method()
{
if (!IsAuthorize()) return;
}
private bool IsAuthorize()
{
return SignInManager.IsSignedIn((ClaimsPrincipal)Context.User);
}
}
但是,Hub.Context.User中存在AuthenticationType==null
(因此,IsSignedIn返回false)
我知道在控制器操作中,该字段是从HttpContext实例挖掘的。问题是如何在Hub类中将其手动设置为IsSignedIn returns true?对于我来说,问题是因为
Cookies.applicationcokie.AutomaticAuthenticate=false代码>
即,在启动类中,标识配置应为
services.Configure<IdentityOptions>(options => {
// other configs
options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
});
services.Configure(选项=>{
//其他配置
options.Cookies.applicationcokie.AutomaticAuthenticate=true;
});
因此,当客户端请求一个集线器方法时,它将通过身份验证进行身份验证<代码>SignInManager.IsSignedIn如果成功,则返回true
services.Configure<IdentityOptions>(options => {
// other configs
options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
});