SignalR-Web客户端如何通过基本身份验证和SSL连接到自托管集线器

问题描述 投票:3回答:2

我们有一个同时使用基本身份验证和SSL的自托管(在控制台应用程序中)SignalR集线器。

中心类是:

[HubName("TestingHub")]
[Authorize(Mode=AuthorizeMode.Both)]
public class TestingHub : Hub
{
    public void TestMethod(int arg)
    {
        Console.WriteLine("Arg: {0}", arg); 
    }

    public string TestWebClientCall(string message)
    {
        Clients.Caller.clientFunction(string.Format("From the server : {0}", message));
        return "Call Worked";
    }
}

自托管操作如下:

var url = "https://localhost:3232/";
var server = new Server(url);
server.Configuration.DisconnectTimeout = TimeSpan.Zero;
var authoriser = new Authoriser();
server.HubPipeline.AddModule(new AuthorizeModule(authoriser, authoriser));
server.AuthenticationSchemes = AuthenticationSchemes.Basic;
server.MapHubs();            
server.Start();

[授权者类是:

public class Authoriser : IAuthorizeHubConnection, IAuthorizeHubMethodInvocation
{
    bool isAuthorised(HttpListenerBasicIdentity identity)
    {
        var authorised = Membership.Provider.ValidateUser(identity.Name, identity.Password);
        return authorised;
    }

    public bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request)
    {
        var identity = (HttpListenerBasicIdentity)request.User.Identity;
        return isAuthorised(identity);
    }

    public bool AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext)
    {
        var identity = (HttpListenerBasicIdentity)hubIncomingInvokerContext.Hub.Context.User.Identity;
        return isAuthorised(identity);
    }
}

然后,MVC Razor页面javascript如下:

$(document).ready(function () {
    var hubUrl = "https://localhost:3232/";
    $.connection.hub.url = hubUrl;
    var hub = $.connection.TestingHub;
    if (hub == undefined) {
        alert("hub not found at " + hubUrl);
    }
    else {

        $.extend(hub, {
            clientFunction: function (textMessage) {
                alert("clientFunction called : " + textMessage);
            }
        });

        $.connection.hub.start()
            .done(function() {
                hub.server.testWebClientCall('Hello from the client')
                .done(function (message) {
                    alert(message);
                });
            })
            .fail(function() {
                alert("could not connect!");
            });
    }

});

发生了什么:

  1. 页面加载并弹出基本身份验证登录框-用户名/密码已放入
  2. 再次弹出基本身份验证登录框-我猜是SignalR /集线器包含的脚本
  3. 'hub'对象有效-即可以看到'TestingHub'
  4. $。connection.hub.start()调用总是转到.fail,甚至从未尝试过hub.server.testWebClientCall。

由于我们可以从.NET控制台应用程序客户端访问,因此SSL证书对于自托管来说都设置得很好。

所以问题确实是,如何针对同时包含基本身份验证和SSL的自托管集线器来完成此操作?如何将用户名/密码组合传递到SignalR集线器/呼叫以通过身份验证?

作为参考,我们正在测试这种方法,因为我们当前有一个MVC3站点,该站点通过HTTPS / SSL上的表单身份验证进行安全保护,并且根据我的另一个问题,可以访问非安全的自托管SignalR集线器(即非HTTPS / SSL)从HTTP / SSL下的MVC站点似乎不起作用。

[在SignalR样本中,我找到了有关'托管'(即AuthHub类)的授权的详细信息,但是我找不到有关如何从Web客户端进行连接的任何信息-似乎缺少'真正的世界的样本-即具有完全身份验证和SSL加密。

ssl https forms-authentication signalr basic-authentication
2个回答
1
投票

我认为这与自托管或SSL无关。我想您是在问如何将用户名和密码传递给期望通过javascript(“网络客户端”)进行基本身份验证的服务。无法使用SignalR JS API更改标头,所以我们很幸运。

执行类似的操作可能会帮助:

http://coderseye.com/2007/how-to-do-http-basic-auth-in-ajax.html

您可以尝试使用$ .ajaxSetup影响所有传出的ajax请求,但我不确定这对于websockets是否有效。

似乎websockets不支持Auhtorization标头:

basic authentication for websockets

因此,您将不得不使用查询字符串。


0
投票

在建立SignalR连接之前,请使用以下代码:

    $.ajaxSetup({
      headers: {
        'Authorization': "your auth token"
      }
    });

好消息是:SignalR与auth一起使用!

坏消息是:SignalR从websocket退回长时间的投票。

我更喜欢这样做,因为我根本无法接受url或方法调用中的任何丑陋参数!

© www.soinside.com 2019 - 2024. All rights reserved.