这让我疯了.
我正在使用最新的signalR版本(2.0.2).这是我的集线器代码(OnConnected)
public override Task OnConnected()
{
//User is null then Identity and Name too.
Connections.Add(Context.User.Identity.Name, Context.ConnectionId);
return base.OnConnected();
}
Run Code Online (Sandbox Code Playgroud)
这是我的Controller的登录方法:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UnitOfWork.UserRepository.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
}
TempData["ErrorMessage"] = Resources.InvalidUserNameOrPassword;
// If we got this far, something failed, redisplay form
return RedirectToAction("Index","Home");
}
Run Code Online (Sandbox Code Playgroud)
我发现有些人在OnDisconnected上遇到这个问题,我甚至都没有.
我正在使用MCV5模板.
你知道什么是错的吗?
每当我尝试Context.User.Identity从 a访问时Hub,用户详细信息始终为空。
注: Context.User并没有返回null,该属性只是一句:
我已经查看了许多与此类似的关于 SO 的问题,但他们似乎都在问为什么Context.Useris null,答案是移到app.MapSignalR();下面ConfigureAuth();,正如我所做的那样:
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.MapSignalR();
}
Run Code Online (Sandbox Code Playgroud)
该HttpContext.Current.User还返回空,但同样,只有从内Hub。如果我从任何其他人调用它Controller,我会得到当前登录的用户详细信息。
最后,我也觉得令人不安的HttpContext.Current.User.IsAuthenticated是false,用户仍然可以访问该功能。(请参阅Authorize上面屏幕截图中的属性。)
有没有人能够解释为什么我的用户是空的?
非常感谢所有帮助。