示例#1
0
        public async Task <NegotiationResponse> NegotiateAsync(string hubName, HttpContext httpContext = null, string userId = null, IEnumerable <Claim> claims = null, TimeSpan?lifetime = null, bool isDiagnosticClient = false, CancellationToken cancellationToken = default)
        {
            try
            {
                if (cancellationToken == default && httpContext != null)
                {
                    cancellationToken = httpContext.RequestAborted;
                }

                var candidateEndpoints = _serviceEndpointManager.GetEndpoints(hubName);
                var selectedEndpoint   = _router.GetNegotiateEndpoint(httpContext, candidateEndpoints);
                var provider           = _serviceEndpointManager.GetEndpointProvider(selectedEndpoint);

                Func <IEnumerable <Claim> > claimProvider = null;
                if (claims != null)
                {
                    claimProvider = () => claims;
                }
                var claimsWithUserId = ClaimsUtility.BuildJwtClaims(httpContext?.User, userId: userId, claimProvider, isDiagnosticClient: isDiagnosticClient);

                var tokenTask = provider.GenerateClientAccessTokenAsync(hubName, claimsWithUserId, lifetime);
                await tokenTask.OrTimeout(cancellationToken, Timeout, GeneratingTokenTaskDescription);

                return(new NegotiationResponse
                {
                    Url = provider.GetClientEndpoint(hubName, null, null),
                    AccessToken = tokenTask.Result
                });
            }
            catch (Exception e)
            {
                throw new AzureSignalRException(ErrorMsg, e);
            }
        }
示例#2
0
 public ServiceManager(RestClientFactory restClientFactory, ServiceHubContextFactory serviceHubContextFactory, IServiceEndpointManager endpointManager, IServiceProvider serviceProvider)
 {
     _restClientFactory        = restClientFactory;
     _serviceHubContextFactory = serviceHubContextFactory;
     _serviceProvider          = serviceProvider;
     _endpoint         = endpointManager.Endpoints.Single().Key;
     _endpointProvider = endpointManager.GetEndpointProvider(_endpoint);
 }
示例#3
0
 public ServiceManager(IReadOnlyCollection <ServiceDescriptor> services, IServiceProvider serviceProvider, RestClientFactory restClientFactory, IServiceEndpointManager endpointManager)
 {
     _services          = services;
     _serviceProvider   = serviceProvider;
     _restClientFactory = restClientFactory;
     _endpoint          = endpointManager.Endpoints.Keys.First();
     _endpointProvider  = endpointManager.GetEndpointProvider(_endpoint);
 }
示例#4
0
        public ServiceManager(IReadOnlyCollection <ServiceDescriptor> services, IServiceProvider serviceProvider, RestClientFactory restClientFactory, IServiceEndpointManager endpointManager, IOptions <ServiceManagerOptions> options)
        {
            _services          = services;
            _serviceProvider   = serviceProvider;
            _restClientFactory = restClientFactory;
            var connectionString = options.Value.ConnectionString;

            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                _endpoint         = new ServiceEndpoint(connectionString);
                _endpointProvider = endpointManager.GetEndpointProvider(_endpoint);
            }
        }
示例#5
0
        private static IServiceConnectionContainer CreateContainer(IServiceConnectionFactory serviceConnectionFactory, ServiceEndpoint endpoint, string hub, int count, IServiceEndpointManager endpointManager, IServerNameProvider nameProvider, ILoggerFactory loggerFactory)
        {
            var provider          = endpointManager.GetEndpointProvider(endpoint);
            var connectionFactory = new ConnectionFactory(hub, provider, nameProvider, loggerFactory);

            if (endpoint.EndpointType == EndpointType.Primary)
            {
                return(new StrongServiceConnectionContainer(serviceConnectionFactory, connectionFactory, count, endpoint));
            }
            else
            {
                return(new WeakServiceConnectionContainer(serviceConnectionFactory, connectionFactory, count, endpoint));
            }
        }
示例#6
0
        public async Task <NegotiationResponse> NegotiateAsync(string hubName, NegotiationOptions negotiationOptions, CancellationToken cancellationToken = default)
        {
            negotiationOptions ??= NegotiationOptions.Default;
            var httpContext          = negotiationOptions.HttpContext;
            var userId               = negotiationOptions.UserId;
            var claims               = negotiationOptions.Claims;
            var isDiagnosticClient   = negotiationOptions.IsDiagnosticClient;
            var enableDetailedErrors = negotiationOptions.EnableDetailedErrors;
            var lifetime             = negotiationOptions.TokenLifetime;

            try
            {
                if (cancellationToken == default && httpContext != null)
                {
                    cancellationToken = httpContext.RequestAborted;
                }

                var candidateEndpoints = _serviceEndpointManager.GetEndpoints(hubName);
                var selectedEndpoint   = _router.GetNegotiateEndpoint(httpContext, candidateEndpoints);
                var provider           = _serviceEndpointManager.GetEndpointProvider(selectedEndpoint);

                Func <IEnumerable <Claim> > claimProvider = null;
                if (claims != null)
                {
                    claimProvider = () => claims;
                }
                var claimsWithUserId = ClaimsUtility.BuildJwtClaims(httpContext?.User, userId: userId, claimProvider, enableDetailedErrors: enableDetailedErrors, isDiagnosticClient: isDiagnosticClient);

                var tokenTask = provider.GenerateClientAccessTokenAsync(hubName, claimsWithUserId, lifetime);
                await tokenTask.OrTimeout(cancellationToken, Timeout, GeneratingTokenTaskDescription);

                return(new NegotiationResponse
                {
                    Url = provider.GetClientEndpoint(hubName, null, null),
                    AccessToken = tokenTask.Result
                });
            }
            catch (Exception e) when(e is OperationCanceledException || e is TimeoutException)
            {
                throw new AzureSignalRException(ErrorMsg, e);
            }
        }
        public NegotiationResponse Process(HttpContext context, string hubName)
        {
            var claims       = BuildClaims(context);
            var request      = context.Request;
            var originalPath = GetOriginalPath(request.Path);
            var provider     = _endpointManager.GetEndpointProvider(_router.GetNegotiateEndpoint(_endpointManager.GetAvailableEndpoints()));

            if (provider == null)
            {
                throw new InvalidOperationException("No endpoint available.");
            }

            return(new NegotiationResponse
            {
                Url = provider.GetClientEndpoint(hubName, originalPath,
                                                 request.QueryString.HasValue ? request.QueryString.Value.Substring(1) : string.Empty),
                AccessToken = provider.GenerateClientAccessToken(hubName, claims),
                // Need to set this even though it's technically protocol violation https://github.com/aspnet/SignalR/issues/2133
                AvailableTransports = new List <AvailableTransport>()
            });
        }
        public async Task <NegotiationResponse> Process(HttpContext context, string hubName)
        {
            var claims       = BuildClaims(context);
            var request      = context.Request;
            var cultureName  = context.Features.Get <IRequestCultureFeature>()?.RequestCulture.Culture.Name;
            var originalPath = GetOriginalPath(request.Path);
            var provider     = _endpointManager.GetEndpointProvider(_router.GetNegotiateEndpoint(context, _endpointManager.GetEndpoints(hubName)));

            if (provider == null)
            {
                return(null);
            }

            var queryString = GetQueryString(request.QueryString.HasValue ? request.QueryString.Value.Substring(1) : null, cultureName);

            return(new NegotiationResponse
            {
                Url = provider.GetClientEndpoint(hubName, originalPath, queryString),
                AccessToken = await provider.GenerateClientAccessTokenAsync(hubName, claims),
                // Need to set this even though it's technically protocol violation https://github.com/aspnet/SignalR/issues/2133
                AvailableTransports = new List <AvailableTransport>()
            });
        }
        public NegotiationResponse Process(HttpContext context, string hubName)
        {
            var claims       = BuildClaims(context);
            var request      = context.Request;
            var originalPath = GetOriginalPath(request.Path);
            var provider     = _endpointManager.GetEndpointProvider(_router.GetNegotiateEndpoint(context, _endpointManager.GetEndpoints(hubName)));

            if (provider == null)
            {
                return(null);
            }

            var clientRequestId = _connectionRequestIdProvider.GetRequestId();

            var queryString = GetQueryString(_connectionRequestIdProvider, request.QueryString.HasValue ? request.QueryString.Value.Substring(1) : null);

            return(new NegotiationResponse
            {
                Url = provider.GetClientEndpoint(hubName, originalPath, queryString),
                AccessToken = provider.GenerateClientAccessToken(hubName, claims, requestId: clientRequestId),
                // Need to set this even though it's technically protocol violation https://github.com/aspnet/SignalR/issues/2133
                AvailableTransports = new List <AvailableTransport>()
            });
        }