asp.net-mvc - SignalR 2.X.X 的 Context.User.Identity.Name 为 null。如何修复它?

标签 asp.net-mvc authentication signalr signalr-hub owin

这让我发疯。

我正在使用最新的 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();
        }

这是我的 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");
        }

我发现有些人在 OnDisconnected 上遇到了这个问题,我什至没有做到这一点。

我正在使用 MCV5 模板。

你知道出了什么问题吗?

最佳答案

我找到了最终的解决方案,这是我的OWIN启动类的代码:

        public void Configuration(IAppBuilder app)
        {
        app.MapSignalR();

        // Enable the application to use a cookie to store information for the signed i user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Home/Index")
        });

        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        app.UseMicrosoftAccountAuthentication(new MicrosoftProvider().GetAuthenticationOptions());
        app.UseTwitterAuthentication(new TwitterProvider().GetAuthenticationOptions());
        app.UseFacebookAuthentication(new FacebookProvider().GetAuthenticationOptions());
        app.UseGoogleAuthentication(new GoogleProvider().GetAuthenticationOptions());    
    }

给自己泡杯咖啡,我想“在身份验证后映射 SignalR 怎么样?瞧! 现在它按预期工作了。

        public void Configuration(IAppBuilder app)
        {
        // Enable the application to use a cookie to store information for the signed i user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Home/Index")
        });

        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        app.UseMicrosoftAccountAuthentication(new MicrosoftProvider().GetAuthenticationOptions());
        app.UseTwitterAuthentication(new TwitterProvider().GetAuthenticationOptions());
        app.UseFacebookAuthentication(new FacebookProvider().GetAuthenticationOptions());
        app.UseGoogleAuthentication(new GoogleProvider().GetAuthenticationOptions());

        app.MapSignalR();    
    }

关于asp.net-mvc - SignalR 2.X.X 的 Context.User.Identity.Name 为 null。如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22002092/

相关文章:

javascript - 如何在SignalR客户端中设置连接ID?

c# - 保存用户消息的静态字典

asp.net - Orchard CMS 如何进行日志记录?

node.js - 使用node.js和passport进行身份验证后重定向到上一页

Spring Login On HTTPS 在验证后重定向到 HTTP

cordova - 服务器到 Phonegap 推送 : SignalR vs Azure Notification Hub

c# - 如何使用 asp.net mvc 3 填充 jqgrid

javascript - 从 MVC AJAX 调用获取 "Invalid JSON primitive"错误

php - 我设置 PHP session 的方式是否存在安全问题?

c# - 无法在 Android 上获取 SignalA 以连接到 SignalR 后端