Asp.net-Mvc

SignalR 2.XX 的 Context.User.Identity.Name 為 null 如何解決?

  • February 25, 2014

這讓我發瘋。

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

這是我的控制器的登錄方法:

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

引用自:https://stackoverflow.com/questions/22002092