Azure 移动服务需要对 SignalR HTML/Javascript 客户端进行身份验证

Azure Mobile Service requires authentication for SignalR HTML/Javascript Client

我在 localhost:52253 本地有一个新的 Azure 移动服务 运行。我安装了最新的 SignalR Nuget 包:Azure Mobile Services .NET Backend SignalR Extension 1.0.450

我还有一个测试 HTML/Javascript 客户端来自 localhost:54697/

解决 COR 问题后,我仍然无法连接到 signalR 集线器,因为从 SignalR javascript 客户端到 localhost:52232/signalr/negotiate 移动服务的协商请求总是结果在 HTTP/1.1 401 未经授权。

我没有在服务器上启用任何身份验证,一切都是默认的。我还确认 WebApi 控制器无需任何身份验证即可工作。我还尝试用以下方法装饰集线器和所有集线器方法:

[AuthorizeLevel(AuthorizationLevel.Anonymous)]
public class ChatHub : Hub
{
    public ApiServices Services { get; set; }

    [AuthorizeLevel(AuthorizationLevel.Anonymous)]
    public string Send(string message)
    {
        return "Hello from SignalR Chat Hub!";
    }
}

通过 fiddler 的确切请求是:

GET http://localhost:52253/signalr/negotiate?clientProtocol=1.4&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&_=1434677053589 HTTP/1.1
Host: localhost:52253
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/plain, */*; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: http://localhost:52253
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: ai_user=09b8290770484a41bec47ce63b379abf|2015-04-01T02:18:46.1194121+00:00; ai_session=ab23831e90cb4773b79353cd4e2963ff|2015-04-28T01:14:18.415Z|2015-04-28T01:52:17.864Z


HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/10.0
X-Content-Type-Options: nosniff
WWW-Authenticate: Basic realm="Service"
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNca2Fsd2VfMDAwXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTNcUHJvamVjdHNcVGVzdFNpZ25hbFIuTW9iaWxlU2VydmljZVxUZXN0U2lnbmFsUi5Nb2JpbGVTZXJ2aWNlXHNpZ25hbHJcbmVnb3RpYXRl?=
X-Powered-By: ASP.NET
Date: Fri, 19 Jun 2015 04:32:28 GMT
Content-Length: 0

然后浏览器弹出一个表单,要求进行基本身份验证,但我没有用户名或密码。这是本地 运行,因此也没有 Azure 应用程序密钥。

如何禁用或解决 Azure 移动服务上 SignalR 中心的身份验证要求?

你看过这个博客吗post? http://blogs.msdn.com/b/azuremobile/archive/2014/05/30/realtime-with-signalr-and-azure-mobile-net-backend.aspx

您可以通过以下方式全局设置授权:

public static void Register()
   {
       // Initialize SignalR
       SignalRExtensionConfig.Initialize();

       // Use this class to set configuration options for your mobile service
       ConfigOptions options = new ConfigOptions();
       options.SetRealtimeAuthorization(AuthorizationLevel.Anonymous);

       ...
   }