Beispiel #1
0
        static async Task Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: subscriber <connectionString>");
                return;
            }

            var connectionString = args[0];
            var hub  = "test01";
            var user = "******";

            var serviceClient = new WebPubSubServiceClient(connectionString, hub);
            var url           = serviceClient.GetClientAccessUri(userId: user);

            using (var client = new WebsocketClient(url, () =>
            {
                var inner = new ClientWebSocket();
                inner.Options.AddSubProtocol("json.webpubsub.azure.v1");
                return(inner);
            }))
            {
                client.MessageReceived.Subscribe(msg => Console.WriteLine($"Message received: {msg}"));
                await client.Start();

                Console.WriteLine("Connected.");
                Console.Read();
            }
        }
Beispiel #2
0
        static async Task Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: subscriber <connectionString> <hub>");
                return;
            }
            var connectionString = args[0];
            var hub = args[1];

            // Either generate the URL or fetch it from server or fetch a temp one from the portal
            var serviceClient = new WebPubSubServiceClient(connectionString, hub);
            var url           = serviceClient.GetClientAccessUri();

            using (var client = new WebsocketClient(url, () =>
            {
                var inner = new ClientWebSocket();
                inner.Options.AddSubProtocol("json.webpubsub.azure.v1");
                return(inner);
            }))
            {
                client.MessageReceived.Subscribe(msg => Console.WriteLine($"Message received: {msg}"));
                await client.Start();

                Console.WriteLine("Connected.");
                Console.Read();
            }
        }
        public void TestGenerateUriUseSameKidWithSameKey(string connectionString)
        {
            var serviceClient = new WebPubSubServiceClient(" Endpoint=http://localhost;Port=8080;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGH;Version=1.0;", "hub");
            var uri1          = serviceClient.GetClientAccessUri();
            var uri2          = serviceClient.GetClientAccessUri();

            Assert.AreEqual("localhost:8080", uri1.Authority);
            Assert.AreEqual("/client/hubs/hub", uri1.AbsolutePath);
            var token1 = HttpUtility.ParseQueryString(uri1.Query).Get("access_token");

            Assert.NotNull(token1);
            var token2 = HttpUtility.ParseQueryString(uri2.Query).Get("access_token");

            Assert.NotNull(token2);
            var jwt1 = JwtTokenHandler.ReadJwtToken(token1);
            var jwt2 = JwtTokenHandler.ReadJwtToken(token2);

            Assert.AreEqual(jwt1.Header.Kid, jwt2.Header.Kid);
        }
        public void TestGenerateUriUseSameKidWithSameKey(string connectionString, string hub, string expectedUrl)
        {
            var serviceClient = new WebPubSubServiceClient(string.Format(connectionString, FakeAccessKey), hub);
            var uri1          = serviceClient.GetClientAccessUri();
            var uri2          = serviceClient.GetClientAccessUri();
            var urlBuilder    = new UriBuilder(uri1);

            urlBuilder.Query = string.Empty;
            Assert.AreEqual(expectedUrl, urlBuilder.Uri.ToString());
            var token1 = HttpUtility.ParseQueryString(uri1.Query).Get("access_token");

            Assert.NotNull(token1);
            var token2 = HttpUtility.ParseQueryString(uri2.Query).Get("access_token");

            Assert.NotNull(token2);
            var jwt1 = JwtTokenHandler.ReadJwtToken(token1);
            var jwt2 = JwtTokenHandler.ReadJwtToken(token2);

            Assert.AreEqual(jwt1.Header.Kid, jwt2.Header.Kid);
        }
        public void TestGenerateUriContainsExpectedPayloadsDto(string userId, string[] roles)
        {
            var serviceClient = new WebPubSubServiceClient(string.Format("Endpoint=http://localhost;Port=8080;AccessKey={0};Version=1.0;", FakeAccessKey), "hub");
            var expiresAt     = DateTimeOffset.UtcNow + TimeSpan.FromMinutes(5);
            var uri           = serviceClient.GetClientAccessUri(expiresAt, userId, roles);
            var token         = HttpUtility.ParseQueryString(uri.Query).Get("access_token");

            Assert.NotNull(token);
            var jwt = JwtTokenHandler.ReadJwtToken(token);

            var audience = jwt.Claims.FirstOrDefault(s => s.Type == "aud");

            Assert.NotNull(audience);
            Assert.AreEqual("http://localhost:8080/client/hubs/hub", audience.Value);
            var iat = jwt.Claims.FirstOrDefault(s => s.Type == "iat")?.Value;

            Assert.NotNull(iat);
            Assert.IsTrue(long.TryParse(iat, out var issuedAt));
            var exp = jwt.Claims.FirstOrDefault(s => s.Type == "exp")?.Value;

            Assert.NotNull(exp);
            Assert.IsTrue(long.TryParse(exp, out var expireAt));

            // default expire after should be ~5 minutes (~300 seconds)
            var expireAfter = expireAt - issuedAt;

            Assert.IsTrue(expireAfter > 295 && expireAfter < 305);

            var sub = jwt.Claims.Where(s => s.Type == "sub").Select(s => s.Value).ToArray();

            if (userId != null)
            {
                Assert.AreEqual(1, sub.Length);
                Assert.AreEqual(userId, sub[0]);
            }
            else
            {
                Assert.IsEmpty(sub);
            }

            var roleClaims = jwt.Claims.Where(s => s.Type == "role").Select(s => s.Value).ToArray();

            if (roles?.Length > 0)
            {
                Assert.AreEqual(roles, roleClaims);
            }
            else
            {
                Assert.IsEmpty(roleClaims);
            }
        }
        internal WebPubSubConnection GetClientConnection(string userId = null, string[] roles = null)
        {
            var url = _client.GetClientAccessUri(userId, roles);

            #region TODO: Remove after SDK fix. Work-around to support http.
            if (!_serviceConfig.Endpoint.Scheme.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                var replaced = url.AbsoluteUri.Replace("wss", "ws");
                url = new Uri(replaced);
            }
            #endregion

            return(new WebPubSubConnection(url));
        }
Beispiel #7
0
        static async Task Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("Usage: subscriber <endpoint> <key> <hub>");
                return;
            }
            var endpoint = args[0];
            var key      = args[1];
            var hub      = args[2];

            // Either generate the URL or fetch it from server or fetch a temp one from the portal
            var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), hub, new Azure.AzureKeyCredential(key));
            var url           = serviceClient.GetClientAccessUri(claims: new System.Security.Claims.Claim[] {
                new System.Security.Claims.Claim("sub", "userId")
            });

            // start the connection
            using var client = new WebSocketClient(url, (message, type) =>
            {
                Console.WriteLine(Encoding.UTF8.GetString(message.Span));
                return(default);
            },
Beispiel #8
0
        internal WebPubSubConnection GetClientConnection(string userId = null, string[] roles = null)
        {
            var url = _client.GetClientAccessUri(userId: userId, roles: roles);

            return(new WebPubSubConnection(url));
        }