예제 #1
0
        public static IActionResult Negotiate(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", "options", Route = "{hub}/negotiate")] HttpRequest req,
            string hub,
            TraceWriter log)
        {
            var(endpoint, accessKey) = ConnectionStringHelpers.ParseSignalRConnectionString();
            var token = new JwtBuilder()
                        .WithAlgorithm(new HMACSHA256Algorithm())
                        .WithSecret(accessKey)
                        .AddClaim("exp", DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds())
                        .AddClaim("aud", $"{endpoint}:5001/client/?hub={hub}")
                        .Build();

            return(new OkObjectResult(new
            {
                url = $"{endpoint}:5001/client/?hub={hub}",
                accessToken = token,
                availableTransports = Enumerable.Empty <string>()
            }));
        }
예제 #2
0
        private static async Task SignalRBroadcast(string target, params string[] arguments)
        {
            var(endpoint, accessKey) = ConnectionStringHelpers.ParseSignalRConnectionString();
            var token = new JwtBuilder()
                        .WithAlgorithm(new HMACSHA256Algorithm())
                        .WithSecret(accessKey)
                        .AddClaim("exp", DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds())
                        .AddClaim("aud", $"{endpoint}:5002/api/v1-preview/hub/notifications")
                        .Build();
            var httpClient = new HttpClient
            {
                BaseAddress = new Uri($"{endpoint}:5002")
            };

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            await httpClient.PostAsJsonAsync("/api/v1-preview/hub/notifications", new
            {
                target,
                arguments
            });
        }