2015-11-01 47 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之前。謝謝!現在起作用了 –