Example #1
0
        internal HostContext GetHostContext(OpenConnectionMessage message, Stream responseStream, IServiceConnection serviceConnection)
        {
            var connectionId = message.ConnectionId;
            var context      = new OwinContext();
            var response     = context.Response;
            var request      = context.Request;

            response.Body = responseStream;

            var user = request.User = message.GetUserPrincipal();

            request.Path = new PathString("/");

            var userToken = string.IsNullOrEmpty(user.Identity.Name) ? string.Empty : ":" + user.Identity.Name;

            // TODO: when https://github.com/SignalR/SignalR/issues/4175 is resolved, we can get rid of paring query string
            var queryCollection = HttpUtility.ParseQueryString(message.QueryString ?? string.Empty);

            queryCollection[AspNetConstants.QueryString.ConnectionToken] = $"{connectionId}{userToken}";

            request.QueryString = new QueryString(queryCollection.ToString());

            if (message.Headers != null)
            {
                foreach (var pair in message.Headers)
                {
                    request.Headers.Add(pair.Key, pair.Value);
                }
            }

            context.Environment[AspNetConstants.Context.AzureServiceConnectionKey] = serviceConnection;
            return(new HostContext(context.Environment));
        }
Example #2
0
        internal HostContext GetHostContext(OpenConnectionMessage message, Stream responseStream)
        {
            var connectionId = message.ConnectionId;
            var context      = new OwinContext();
            var response     = context.Response;
            var request      = context.Request;

            response.Body = responseStream;

            var user = request.User = message.GetUserPrincipal();

            request.Path = new PathString("/");

            string queryString = message.QueryString;

            if (queryString.Length > 0)
            {
                // The one from Azure SignalR always contains a leading '?' character however the Owin one does not
                if (queryString[0] == '?')
                {
                    queryString = queryString.Substring(1);
                }

                request.QueryString = new QueryString(queryString);
            }

            if (message.Headers != null)
            {
                foreach (var pair in message.Headers)
                {
                    request.Headers.Add(pair.Key, pair.Value);
                }
            }
            return(new HostContext(context.Environment));
        }
Example #3
0
        public void TestGetUserPrincipal(string expectedAuthenticationType, string expectedUserName, int expectedUserClaimCount, params string[] claims)
        {
            var message   = new OpenConnectionMessage(Guid.NewGuid().ToString("N"), claims?.Select(s => new Claim(s, GenerateFakeClaimValueFromKey(s))).ToArray());
            var principal = message.GetUserPrincipal();

            Assert.Equal(expectedAuthenticationType, principal.Identity.AuthenticationType);
            Assert.Equal(expectedUserName, principal.Identity.Name);
            Assert.Equal(expectedUserClaimCount, principal.Claims.Count());
        }