SingalR ASP.NET跨域连接问题

时间:2017-05-30 19:54:11

标签: asp.net model-view-controller cors signalr cross-domain

我正在尝试实现一个提到here的ASP.NET SignalR应用程序。

我已经按照here提到了客户端。对于客户端,我使用没有生成代理的代码。

客户端和服务器在两者位于同一域时成功连接,但在托管跨域时无法通信。虽然已经实现了上述文章中针对跨域提及的代码。由于我的客户端和服务器是在Azure中托管的,因此Azure中是否需要启用跨域通信的设置,或者还有其他我缺少的内容?

这是我得到的错误:

请求的资源上没有“Access-Control-Allow-Origin”标头。因此不允许原点访问。响应的HTTP状态代码为500。

我的启动课程是:

public void Configuration(IAppBuilder app)
    {
        //app.UseCors(CorsOptions.AllowAll);

        // Any connection or hub wire up and configuration should go here
        //app.MapSignalR();
        // Branch the pipeline here for requests that start with "/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);
            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);
        });

    }

客户端代码是:`

        $(function (){
        var ChatServerUrl ="http://chatserverurl.net/home/";
        var ChatUrl = ChatServerUrl + "signalr";

        var connection = $.hubConnection(ChatUrl, { useDefaultPath: false });

        connection.logging = true;
        var chatHubProxy = connection.createHubProxy('chatHub');

        chatHubProxy.on('addNewMessageToPage', function (name, message) {

            console.log("AddNewMessageToPage Function!");

            // Add the message to the page.
            $('#discussion').append('<li><strong>' + htmlEncode(name)
                + '</strong>: ' + htmlEncode(message) + '</li>');
        });

        // Get the user name and store it to prepend to messages.
        $('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.
        $('#message').focus();
        // Start the connection.
        //connection.start({ withCredentials : false }).done(function () {
        connection.start({ withCredentials: true }).done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chatHubProxy.invoke('Send', $('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });

            console.log("SignalR Connected!");
        });
    });`

1 个答案:

答案 0 :(得分:1)