使用javascript客户端进行SignalR身份验证 [英] SignalR authentication with javascript client

查看:629
本文介绍了使用javascript客户端进行SignalR身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MVC5 SignalR 开放身份验证 $ C>。我使用javascript客户端在 SignalR 上调用一个简单的服务器方法,并从Server接收回复。它运行良好,但如果我添加 [Authorize] 标记,它甚至不会调用服务器方法(调试时没有得到任何响应)。

I was playing with the open authentication in MVC5 and SignalR. I use a javascript client to call a simple server method on SignalR and receive a reply from Server. It works well, but if I add the [Authorize] tag, it does not even call the server method (did not get any response while debugging).

我的假设是服务器将使用身份验证机制来挑战客户端。我错过了什么吗?我是否必须从客户端手动验证用户身份,如果是,我如何传递身份验证令牌?

My assumption was the server will use the Authentication mechanism to challenge the client. Am I missing anything? Do I have to manually authenticate the user from the client side and if so how do I pass the authentication token?

这是我的集线器

    [HubName("authChatHub")]
    public class AuthChatHub : Hub
    {
        [Authorize]
        public void Ping()
        {
            Clients.Caller.Pong("Connection is FINE!!");

            Clients.Caller.Pong(Context.User == null 
              ? "Null user" 
              : Context.User.Identity.IsAuthenticated.ToString());
        }
    }

这是我的 Startup.Auth .cs

    public void ConfigureAuth(IAppBuilder app)
        {
           app.UseGoogleAuthentication();
        }

这是 Startup.cs ,使用代码启用CORS。

Here's the Startup.cs, using the code to enable CORS.

public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app); //added this after a suggestion here, not sure if this is the right place. 

            app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {
                    // EnableJSONP = true //empty for now
                };

                map.RunSignalR(hubConfiguration);
            });
        }
    }

最后这个客户端边代码调用 hub 方法并侦听服务器RPC。

And finally this client side code calls the hub method and listens to the server RPC.

this.sendMessage = () => {
            this.authChat.server.ping();
        };
this.authChat.client.pong = (message) => { console.log(message); };


推荐答案

您必须像使用表格或Windows身份验证一样use是任何其他asp.net应用程序。一旦通过身份验证,您的呼叫将以与在集线器上放置 [授权] 属性之前相同的方式工作。

You have to use Forms or windows authentication as you would use is any other asp.net application. Once you are authenticated your calls would work in the same way as they did before you putting [Authorize]attribute on the hub.

SignalR本身不处理身份验证。

SignalR does not itself deal with authentication.

您必须首先进行身份验证然后将令牌发送到服务器,我认为这个链接可以帮助您实现您想要的目标。

You will have to authenticate first then send the token to server, I think this link can help you achieve what you want to do.

这篇关于使用javascript客户端进行SignalR身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆