asp.net-mvc – Context.User.Identity.Name为null与SignalR 2.X.X.如何解决?

这让我疯狂。

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....