2015-11-01 46 views
1

我正在学习ASP.NET Identity和SignalR等其他内容。我能够使用Identity的自定义实现创建自己的身份验证系统。我现在正在尝试在调用覆盖的OnConnected之前将用户认证为SignalR。
用户使用Cookie身份验证进行身份验证后,[System.Web.Mvc.Authorize]属性会成功传递,而[Microsoft.AspNet.SignalR]属性会告诉我用户尚未进行身份验证。从MVC控制器验证SignalR

//This is my Authorize method: 
[HttpGet] 
public ActionResult Authorize() 
{ 
    AuthenticationManager.SignIn(new ClaimsIdentity(new ClaimsPrincipal(User).Claims.ToArray(), "Bearer")); 
    return new EmptyResult(); 
} 

//This is my CookieAuthenticationOptions 
app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
    LoginPath = new PathString("/Account/Login"), 
    Provider = new CookieAuthenticationProvider() 
}); 

//On the Hub 
public override Task OnConnected() 
{ 
    string userName = Context.User.Identity.Name; //User is null? 
} 

我在这里失踪了什么?我已经搜索了一段时间,我无法找到任何东西,甚至没有关于如何验证SignalR的想法。

回答

2

最有可能你的身份验证之前映射SignalR,您必须验证后映射SignalR:

public void Configuration(IAppBuilder app) 
{ 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Home/Index") 
     }); 

     //... 

     app.MapSignalR();  
} 
+0

这就是它!在Startup.cs中的Configuration(IAppBuilder)下,app.MapSignalR()位于ConfigureAuth之前。谢谢!现在起作用了 –