Context.User.Identity.Name is null with SignalR 2.X.X. How to fix it?

后端 未结 5 1502
刺人心
刺人心 2020-11-30 03:12

This is driving me insane.

I\'m using latest signalR release (2.0.2). This is my hub code (OnConnected)

        public override Task OnConnected()
          


        
5条回答
  •  暖寄归人
    2020-11-30 03:24

    I found the final solution, this is the code of my OWIN startup class:

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

    Making myself some coffee, I thought "What about mapping SignalR AFTER the authentication, and voila! Now it's workign as expected.

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

提交回复
热议问题