コード例 #1
0
        public static string CreateNegotiationContent(string connectionId = "00000000-0000-0000-0000-000000000000",
                                                      HttpTransportType?transportTypes = null, string connectionToken = "connection-token", int negotiateVersion = 0)
        {
            var availableTransports = new List <object>();

            transportTypes = transportTypes ?? HttpTransports.All;
            if ((transportTypes & HttpTransportType.WebSockets) != 0)
            {
                availableTransports.Add(new
                {
                    transport       = nameof(HttpTransportType.WebSockets),
                    transferFormats = new[] { nameof(TransferFormat.Text), nameof(TransferFormat.Binary) }
                });
            }
            if ((transportTypes & HttpTransportType.ServerSentEvents) != 0)
            {
                availableTransports.Add(new
                {
                    transport       = nameof(HttpTransportType.ServerSentEvents),
                    transferFormats = new[] { nameof(TransferFormat.Text) }
                });
            }
            if ((transportTypes & HttpTransportType.LongPolling) != 0)
            {
                availableTransports.Add(new
                {
                    transport       = nameof(HttpTransportType.LongPolling),
                    transferFormats = new[] { nameof(TransferFormat.Text), nameof(TransferFormat.Binary) }
                });
            }

            return(JsonConvert.SerializeObject(new { connectionId, availableTransports, connectionToken, negotiateVersion }));
        }
コード例 #2
0
        public static IHubConnectionBuilder WithUrlBlazor(this IHubConnectionBuilder hubConnectionBuilder, Uri url, IJSRuntime jsRuntime,
                                                          HttpTransportType?transports = null, Action <BlazorHttpConnectionOptions> options = null)
        {
            if (hubConnectionBuilder == null)
            {
                throw new ArgumentNullException(nameof(hubConnectionBuilder));
            }

            if (jsRuntime == null)
            {
                throw new ArgumentNullException(nameof(jsRuntime));
            }

            hubConnectionBuilder.Services.Configure <BlazorHttpConnectionOptions>(o =>
            {
                o.Url = url;
                if (!transports.HasValue)
                {
                    return;
                }
                o.Transports = transports.Value;
            });
            if (options != null)
            {
                hubConnectionBuilder.Services.Configure(options);
            }

            hubConnectionBuilder.Services.AddSingleton <EndPoint, BlazorHttpConnectionOptionsDerivedHttpEndPoint>();

            hubConnectionBuilder.Services.AddSingleton <IConfigureOptions <BlazorHttpConnectionOptions>, BlazorHubProtocolDerivedHttpOptionsConfigurer>();

            hubConnectionBuilder.Services.AddSingleton(provider => BuildBlazorHttpConnectionFactory(provider, jsRuntime));
            return(hubConnectionBuilder);
        }
コード例 #3
0
 public static IHubConnectionBuilder WithUrlBlazor(
     this IHubConnectionBuilder hubConnectionBuilder,
     string url,
     IJSRuntime jsRuntime,
     NavigationManager navigationManager,
     HttpTransportType?transports = null,
     Action <BlazorHttpConnectionOptions>?options = null)
 {
     return(WithUrlBlazor(hubConnectionBuilder, new Uri(url), jsRuntime, navigationManager, transports, options));
 }
コード例 #4
0
        private HubConnection CreateHubConnection(
            string path = null,
            HttpTransportType?transportType = null,
            IHubProtocol protocol           = null,
            ILoggerFactory loggerFactory    = null)
        {
            var hubConnectionBuilder = new HubConnectionBuilder();

            hubConnectionBuilder.WithHubProtocol(protocol);
            hubConnectionBuilder.WithLoggerFactory(loggerFactory);
            hubConnectionBuilder.WithConnectionFactory(GetHttpConnectionFactory(loggerFactory, path, transportType ?? HttpTransportType.LongPolling | HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents));

            return(hubConnectionBuilder.Build());
        }
コード例 #5
0
ファイル: HubConnectionTests.cs プロジェクト: cgyqu/SignalR-1
        private HubConnection CreateHubConnection(
            string path = null,
            HttpTransportType?transportType = null,
            IHubProtocol protocol           = null,
            ILoggerFactory loggerFactory    = null)
        {
            var hubConnectionBuilder = new HubConnectionBuilder();

            hubConnectionBuilder.Services.AddSingleton(protocol);
            hubConnectionBuilder.WithLoggerFactory(loggerFactory);

            var delegateConnectionFactory = new DelegateConnectionFactory(
                GetHttpConnectionFactory(loggerFactory, path, transportType ?? HttpTransportType.LongPolling | HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents),
                connection => ((HttpConnection)connection).DisposeAsync());

            hubConnectionBuilder.Services.AddSingleton <IConnectionFactory>(delegateConnectionFactory);

            return(hubConnectionBuilder.Build());
        }
コード例 #6
0
        private static HttpConnection CreateConnection(
            HttpMessageHandler httpHandler = null,
            ILoggerFactory loggerFactory   = null,
            string url           = null,
            ITransport transport = null,
            ITransportFactory transportFactory       = null,
            HttpTransportType?transportType          = null,
            Func <Task <string> > accessTokenFactory = null)
        {
            var httpOptions = new HttpConnectionOptions
            {
                Transports = transportType ?? HttpTransportType.LongPolling,
                HttpMessageHandlerFactory = (httpMessageHandler) => httpHandler ?? TestHttpMessageHandler.CreateDefault(),
                AccessTokenFactory        = accessTokenFactory,
            };

            if (url != null)
            {
                httpOptions.Url = new Uri(url);
            }

            return(CreateConnection(httpOptions, loggerFactory, transport, transportFactory));
        }
コード例 #7
0
 public static IHubConnectionBuilder WithUrlBlazor(this IHubConnectionBuilder hubConnectionBuilder, Uri url,
                                                   HttpTransportType?transports = null, Action <BlazorHttpConnectionOptions> options = null)
 {
     if (hubConnectionBuilder == null)
     {
         throw new ArgumentNullException(nameof(hubConnectionBuilder));
     }
     hubConnectionBuilder.Services.Configure <BlazorHttpConnectionOptions>(o =>
     {
         o.Url = url;
         if (!transports.HasValue)
         {
             return;
         }
         o.Transports = transports.Value;
     });
     if (options != null)
     {
         hubConnectionBuilder.Services.Configure(options);
     }
     hubConnectionBuilder.Services.AddSingleton <IConnectionFactory, BlazorHttpConnectionFactory>();
     return(hubConnectionBuilder);
 }
コード例 #8
0
        private static IHubConnectionBuilder WithUrlCore(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType?transports, Action <HttpConnectionOptions> configureHttpConnection)
        {
            hubConnectionBuilder.Services.Configure <HttpConnectionOptions>(o =>
            {
                o.Url = url;
                if (transports != null)
                {
                    o.Transports = transports.Value;
                }
            });

            if (configureHttpConnection != null)
            {
                hubConnectionBuilder.Services.Configure(configureHttpConnection);
            }

            hubConnectionBuilder.Services.AddSingleton(services =>
            {
                var value = services.GetService <IOptions <HttpConnectionOptions> >().Value;

                var httpOptions = new HttpOptions
                {
                    HttpMessageHandlerFactory = value.MessageHandlerFactory,
                    Headers            = value._headers != null ? new ReadOnlyDictionary <string, string>(value._headers) : null,
                    AccessTokenFactory = value.AccessTokenFactory,
                    WebSocketOptions   = value.WebSocketOptions,
                    Cookies            = value._cookies,
                    Proxy = value.Proxy,
                    UseDefaultCredentials = value.UseDefaultCredentials,
                    ClientCertificates    = value._clientCertificates,
                    Credentials           = value.Credentials,
                };

                Func <IConnection> createConnection = () => new HttpConnection(
                    value.Url,
                    value.Transports,
                    services.GetService <ILoggerFactory>(),
                    httpOptions);

                return(createConnection);
            });

            return(hubConnectionBuilder);
        }
コード例 #9
0
        private static IHubConnectionBuilder WithUrlCore(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType?transports, Action <HttpConnectionOptions> configureHttpConnection)
        {
            if (hubConnectionBuilder == null)
            {
                throw new ArgumentNullException(nameof(hubConnectionBuilder));
            }

            hubConnectionBuilder.Services.Configure <HttpConnectionOptions>(o =>
            {
                o.Url = url;
                if (transports != null)
                {
                    o.Transports = transports.Value;
                }
            });

            if (configureHttpConnection != null)
            {
                hubConnectionBuilder.Services.Configure(configureHttpConnection);
            }

            hubConnectionBuilder.Services.AddSingleton <IConnectionFactory, HttpConnectionFactory>();
            return(hubConnectionBuilder);
        }
コード例 #10
0
    private static IHubConnectionBuilder WithUrlCore(this IHubConnectionBuilder hubConnectionBuilder, Uri url, HttpTransportType?transports, Action <HttpConnectionOptions>?configureHttpConnection)
    {
        if (hubConnectionBuilder == null)
        {
            throw new ArgumentNullException(nameof(hubConnectionBuilder));
        }

        hubConnectionBuilder.Services.Configure <HttpConnectionOptions>(o =>
        {
            o.Url = url;
            if (transports != null)
            {
                o.Transports = transports.Value;
            }
        });

        if (configureHttpConnection != null)
        {
            hubConnectionBuilder.Services.Configure(configureHttpConnection);
        }

        // Add HttpConnectionOptionsDerivedHttpEndPoint so HubConnection can read the Url from HttpConnectionOptions
        // without the Signal.Client.Core project taking a new dependency on Http.Connections.Client.
        hubConnectionBuilder.Services.AddSingleton <EndPoint, HttpConnectionOptionsDerivedHttpEndPoint>();

        // Configure the HttpConnection so that it uses the correct transfer format for the configured IHubProtocol.
        hubConnectionBuilder.Services.AddSingleton <IConfigureOptions <HttpConnectionOptions>, HubProtocolDerivedHttpOptionsConfigurer>();

        // If and when HttpConnectionFactory is made public, it can be moved out of this assembly and into Http.Connections.Client.
        hubConnectionBuilder.Services.AddSingleton <IConnectionFactory, HttpConnectionFactory>();
        return(hubConnectionBuilder);
    }
コード例 #11
0
        public static IEnumerable <Claim> BuildJwtClaims(
            ClaimsPrincipal user,
            string userId,
            Func <IEnumerable <Claim> > claimsProvider,
            string serverName                   = null,
            ServerStickyMode mode               = ServerStickyMode.Disabled,
            bool enableDetailedErrors           = false,
            int endpointsCount                  = 1,
            int?maxPollInterval                 = null,
            bool isDiagnosticClient             = false, int handshakeTimeout = Constants.Periods.DefaultHandshakeTimeout,
            HttpTransportType?httpTransportType = null)
        {
            if (userId != null)
            {
                yield return(new Claim(Constants.ClaimType.UserId, userId));
            }

            if (serverName != null && mode != ServerStickyMode.Disabled)
            {
                yield return(new Claim(Constants.ClaimType.ServerName, serverName));

                yield return(new Claim(Constants.ClaimType.ServerStickyMode, mode.ToString()));
            }

            if (isDiagnosticClient)
            {
                yield return(new Claim(Constants.ClaimType.DiagnosticClient, "true"));
            }

            if (handshakeTimeout != Constants.Periods.DefaultHandshakeTimeout)
            {
                yield return(new Claim(Constants.ClaimType.CustomHandshakeTimeout, handshakeTimeout.ToString()));
            }

            var authenticationType = user?.Identity?.AuthenticationType;

            // No need to pass it when the authentication type is Bearer
            if (authenticationType != null && authenticationType != DefaultAuthenticationType)
            {
                yield return(new Claim(Constants.ClaimType.AuthenticationType, authenticationType));
            }

            // Trace multiple instances
            if (endpointsCount > 1)
            {
                yield return(new Claim(Constants.ClaimType.ServiceEndpointsCount, endpointsCount.ToString()));
            }

            if (enableDetailedErrors)
            {
                yield return(new Claim(Constants.ClaimType.EnableDetailedErrors, true.ToString()));
            }

            // Return custom NameClaimType and RoleClaimType
            // We can have multiple Identities, for now, choose the default one
            if (user?.Identity is ClaimsIdentity identity)
            {
                var nameType = identity.NameClaimType;
                if (nameType != null && nameType != DefaultNameClaimType)
                {
                    yield return(new Claim(Constants.ClaimType.NameType, nameType));
                }

                var roleType = identity.RoleClaimType;
                if (roleType != null && roleType != DefaultRoleClaimType)
                {
                    yield return(new Claim(Constants.ClaimType.RoleType, roleType));
                }
            }

            // add claim if exists, validation is in DI
            if (maxPollInterval.HasValue)
            {
                yield return(new Claim(Constants.ClaimType.MaxPollInterval, maxPollInterval.Value.ToString()));
            }

            if (httpTransportType.HasValue)
            {
                yield return(new Claim(Constants.ClaimType.HttpTransportType, ((int)httpTransportType).ToString()));
            }

            // return customer's claims
            var customerClaims = claimsProvider == null ? user?.Claims : claimsProvider.Invoke();

            if (customerClaims != null)
            {
                foreach (var claim in customerClaims)
                {
                    // Add AzureSignalRUserPrefix if customer's claim name is duplicated with SignalR system claims.
                    // And split it when return from SignalR Service.
                    if (SystemClaims.Contains(claim.Type))
                    {
                        yield return(new Claim(Constants.ClaimType.AzureSignalRUserPrefix + claim.Type, claim.Value));
                    }
                    else
                    {
                        yield return(claim);
                    }
                }
            }
        }
コード例 #12
0
 public static IHubConnectionBuilder WithUrlBlazor(this IHubConnectionBuilder hubConnectionBuilder, string url,
                                                   HttpTransportType?transports = null, Action <BlazorHttpConnectionOptions> options = null)
 {
     return(WithUrlBlazor(hubConnectionBuilder, new Uri(url), transports, options));
 }