Azure Mobile Service需要对SignalR HTML / Javascript客户端进行身份验证

时间:2015-06-19 04:38:19

标签: azure signalr azure-mobile-services

我在localhost:52253本地运行了一个全新的Azure移动服务。我安装了最新的SignalR Nuget包:Azure移动服务.NET后端SignalR扩展1.0.450

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

解决了CORs问题之后,我仍然无法连接到signalR集线器,因为来自SignalR javascript客户端的协商请求到localhost:52232 / signalr / negotiate的移动服务总是会导致HTTP / 1.1 401 Unauthorized。

我没有在服务器上启用任何身份验证,一切都是默认值。我还确认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集线器的此身份验证要求?

1 个答案:

答案 0 :(得分:1)

你看过这篇博文吗? 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);

       ...
   }