【问题标题】:How can I authenticate signalR in asp.net mvc by using cross domain call?如何使用跨域调用在 asp.net mvc 中验证 signalR?
【发布时间】:2018-08-02 20:08:16
【问题描述】:

我正在asp.net mvc 中处理signalR,我想制作signalR 客户端。我确实调用了 signalR 集线器确实存在并获得授权的 apis 服务器。当我删除授权时调用会进入 signalR 集线器,否则当我使用 javacsript 建立 signalR 连接时,signaR 返回 unathorized。

基本上我使用的是基于令牌的身份验证。我将带有查询字符串的令牌发送到 apis 服务器。但是我没有找到授权signalR的解决方案?

      app.Map("/signalr", map =>
                {
                    // Setup the CORS middleware to run before SignalR.
                    // By default this will allow all origins. You can 
                    // configure the set of origins and/or http verbs by
                    // providing a cors options with a different policy.
                    map.UseCors(CorsOptions.AllowAll);

                    // SignalR Auth0 custom configuration.
                    map.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
                    {
                        Provider = new OAuthBearerAuthenticationProvider()
                        {
                            OnRequestToken = context =>
                            {
                                if (context.Request.Path.Value.StartsWith("/signalr"))
                                {
                                    string bearerToken = context.Request.Query.Get("access_token");
                                    if (bearerToken != null)
                                    {
                                        string[] authorization = new string[] { "bearer " + bearerToken };
                                        context.Request.Headers.Add("Authorization", authorization);
                                    }
                                }

                                return null;
                            }
                        }
                    });


                    var hubConfiguration = new HubConfiguration
                    {
                        // You can enable JSONP by uncommenting line below.
                        // JSONP requests are insecure but some older browsers (and some
                        // versions of IE) require JSONP to work cross domain
                        // EnableJSONP = true
                    };
                    // Run the SignalR pipeline. We're not using MapSignalR
                    // since this branch already runs under the "/signalr"
                    // path.
                    map.RunSignalR(hubConfiguration);
                });
                app.UseWebApi(config);
                GlobalHost.DependencyResolver.Register(
            typeof(SignalRHUB),
            () => new SignalRHUB(new UnitOfWork(new DbFactory())));
                //GlobalHost.HubPipeline.RequireAuthentication();
                //app.MapSignalR();

我想通过使用 jwt 令牌来实现 signalR 授权。如果有人可以告诉我另一个解决方案。

【问题讨论】:

  • 整理格式以获得更好的答案。

标签: asp.net-mvc asp.net-web-api signalr jwt


【解决方案1】:

我有一些问题。这个解决方案对我有用Signalr Auth

【讨论】:

    猜你喜欢
    • 2016-08-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多