2017-08-14 68 views
2

我使用SignalR功能(Gray.microsoft.aspnetcore.signalr.server包)部署Asp.Net Core应用程序,并在Hub类中添加身份验证检查。SignInManager IsSignedIn在SignalR集线器中返回false

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); 
    } 
} 

SignInManager.IsSignedIn回报,尽管我的用户的错误被授权使用Identity方案。

在IsSignedIn方法的源代码我找到的代码(简化):

return principal.Identities.Any(i => i.AuthenticationType == IdentityConstants.ApplicationScheme); 

然而存在Hub.Context.User(所以,IsSignedIn返回false)AuthenticationType == null

我知道在Controller中这个字段是从HttpContext实例挖掘出来的。问题是我怎样才能在Hub类中手动设置它为IsSignedIn返回true?

回答

0

对我来说,问题是因为Cookies.ApplicationCookie.AutomaticAuthenticate = false; 即,在启动类标识配置应该是

services.Configure<IdentityOptions>(options => { 
    // other configs 
    options.Cookies.ApplicationCookie.AutomaticAuthenticate = true; 
}); 

因此,当客户端请求的集线器的方法是与身份验证authomatically。 SignInManager.IsSignedIn如果成功则返回true