/// <summary>
        /// Creates a URI with authentication token.
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roles"></param>
        /// <param name="expiresAfter">Defaults to one hour, if not specified.</param>
        /// <returns></returns>
        public virtual Uri GenerateClientAccessUri(string userId = default, string[] roles = default, TimeSpan expiresAfter = default)
        {
            if (expiresAfter == default)
            {
                expiresAfter = TimeSpan.FromHours(1);
            }

            List <Claim> claims = new List <Claim>();

            if (userId != default)
            {
                var subject = new Claim("sub", userId);
                claims.Add(subject);
            }
            if (roles != default && roles.Length > 0)
            {
                foreach (var role in roles)
                {
                    claims.Add(new Claim("role", role));
                }
            }

            string endpoint = this.endpoint.AbsoluteUri;

            if (!endpoint.EndsWith("/", StringComparison.Ordinal))
            {
                endpoint += "/";
            }
            var audience = $"{endpoint}client/hubs/{hub}";

            string token = WebPubSubAuthenticationPolicy.GenerateAccessToken(audience, claims, _credential, expiresAfter);

            var clientEndpoint = new UriBuilder(endpoint);

            clientEndpoint.Scheme = this.endpoint.Scheme == "http" ? "ws" : "wss";
            var uriString = $"{clientEndpoint}client/hubs/{hub}?access_token={token}";

            return(new Uri(uriString));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a URI with authentication token.
        /// </summary>
        /// <returns></returns>
        public virtual Uri GetClientAccessUri(string userId = default, string[] roles = default, TimeSpan expireAfter = default)
        {
            if (expireAfter == default)
            {
                expireAfter = TimeSpan.FromHours(1);
            }

            List <Claim> claims = new List <Claim>();

            if (userId != default)
            {
                var subject = new Claim("sub", userId);
                claims.Add(subject);
            }
            if (roles != default && roles.Length > 0)
            {
                var jsonArray = BinaryData.FromObjectAsJson(roles).ToString();
                var role      = new Claim("role", jsonArray);
                claims.Add(role);
            }

            string endpoint = _endpoint.AbsoluteUri;

            if (!endpoint.EndsWith("/", StringComparison.Ordinal))
            {
                endpoint += "/";
            }
            var audience = $"{endpoint}client/hubs/{_hub}";

            string token = WebPubSubAuthenticationPolicy.GenerateAccessToken(audience, claims, _credential, expireAfter);

            var clientEndpoint = new UriBuilder(endpoint);

            clientEndpoint.Scheme = "wss";
            var uriString = $"{clientEndpoint}client/hubs/{_hub}?access_token={token}";

            return(new Uri(uriString));
        }