Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SignalR—web客户端如何使用基本身份验证和SSL连接到自托管集线器_Ssl_Https_Forms Authentication_Signalr_Basic Authentication - Fatal编程技术网

SignalR—web客户端如何使用基本身份验证和SSL连接到自托管集线器

SignalR—web客户端如何使用基本身份验证和SSL连接到自托管集线器,ssl,https,forms-authentication,signalr,basic-authentication,Ssl,Https,Forms Authentication,Signalr,Basic Authentication,我们有一个自托管(在控制台应用程序中)信号器中心,它使用基本身份验证和SSL 集线器类别为: [HubName("TestingHub")] [Authorize(Mode=AuthorizeMode.Both)] public class TestingHub : Hub { public void TestMethod(int arg) { Console.WriteLine("Arg: {0}", arg); } public strin

我们有一个自托管(在控制台应用程序中)信号器中心,它使用基本身份验证和SSL

集线器类别为:

[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";
    }
}
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);
    }
}
自托管操作如下所示:

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

});
授权人类别为:

[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";
    }
}
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如下所示:

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

});
下面是发生的情况:

  • 页面加载并弹出“基本身份验证登录”框-输入用户名/密码
  • 基本身份验证登录框再次弹出-我猜脚本包括信号器/集线器
  • “hub”对象有效-即可以看到“TestingHub”
  • $.connection.hub.start()调用始终转到.fail,并且从未尝试调用hub.server.TestWebClient
  • SSL证书对于自托管来说设置良好,因为我们可以从.NET控制台应用程序客户端进行访问

    所以问题是,对于这个同时涉及基本身份验证和SSL的自托管集线器,应该如何做到这一点?如何将用户名/密码组合传递到信号器集线器/呼叫以通过身份验证

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


    在SignalR示例中,我找到了有关“托管”(即AuthHub类)授权的详细信息,但我找不到有关如何从web客户端连接的任何信息-似乎缺少“真实世界”示例,即完全认证和SSL加密。

    我认为这与自主机或SSL无关。我想你是在问,如何将用户名和密码传递给一个需要javascript(web客户端)提供基本身份验证的服务。使用SignalR JS API无法更改头文件,因此您在这里很幸运

    这样做可能会有所帮助:

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

    WebSocket似乎不支持AUHTOILIzation标头:


    因此,您必须使用查询字符串。

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

    $.ajaxSetup({
    标题:{
    “授权”:“您的身份验证令牌”
    }
    });
    
    好消息是:signer与auth一起工作

    坏消息是:Signaler退回到websocket的长轮询


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

    您不能在ASP.NET上托管Signaler有什么原因吗?从HubPipeline中删除AuthorizeModule是否可以防止$.connection.hub.start()失败?那似乎与我无关。您从协商和/或连接请求中收到了什么响应?您好,David,我们现在已经在测试项目中完成了连接工作和服务器/客户端回调工作。一旦用户在页面尝试加载signalr/hubs javascript时输入用户名/密码详细信息,这一切都会起作用。我们现在正尝试使用您建议的方法动态加载它,以获得正确的身份验证头-但它似乎不起作用-由于基本身份验证位于自托管集线器上,因此它总是弹出登录框。只是想知道你有没有试过这个?我不明白。什么是弹出的登录框?哪件事?当页面尝试加载signalr/hubs javascript时,浏览器会弹出登录框,因为在自托管中心上有基本的身份验证。我不知道如何解决这个问题。这是特定于浏览器的行为,与自托管无关。无论如何,我已经回答了这个问题,不想在评论中讨论其他问题。如果你还有更具体的问题,请用jabbr打电话给我。