Beispiel #1
0
        private async Task ConnectAsync(HttpRequest httpRequest, IBot bot, CancellationToken cancellationToken)
        {
            Logger.LogInformation($"Received request for web socket connect.");

            // Grab the auth header from the inbound http request
            var authHeader = httpRequest.Headers["Authorization"];

            // Grab the channelId which should be in the http headers
            var channelIdHeader = httpRequest.Headers["channelid"];

            var authenticationRequestResult = await BotFrameworkAuthentication.AuthenticateStreamingRequestAsync(authHeader, channelIdHeader, cancellationToken).ConfigureAwait(false);

            var connectionId = Guid.NewGuid();

            using (var scope = Logger.BeginScope(connectionId))
            {
                var socket = await httpRequest.HttpContext.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);

                var connection = CreateWebSocketConnection(socket, Logger);

                using (var streamingActivityProcessor = new StreamingActivityProcessor(authenticationRequestResult, connection, this, bot))
                {
                    // Start receiving activities on the socket
                    _streamingConnections.TryAdd(connectionId, streamingActivityProcessor);
                    Log.WebSocketConnectionStarted(Logger);
                    await streamingActivityProcessor.ListenAsync(cancellationToken).ConfigureAwait(false);

                    _streamingConnections.TryRemove(connectionId, out _);
                    Log.WebSocketConnectionCompleted(Logger);
                }
            }
        }
        /// <summary>
        /// Used to connect the adapter to a named pipe.
        /// </summary>
        /// <param name="pipeName">The name of the named pipe.</param>
        /// <param name="bot">The bot instance to use.</param>
        /// <param name="appId">The bot's application id.</param>
        /// <param name="audience">The audience to use for outbound communication. This will vary by cloud environment.</param>
        /// <param name="callerId">The callerId, this may be NULL.</param>
        /// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
        public async Task ConnectNamedPipeAsync(string pipeName, IBot bot, string appId, string audience, string callerId)
        {
            if (string.IsNullOrEmpty(pipeName))
            {
                throw new ArgumentNullException(nameof(pipeName));
            }

            _ = bot ?? throw new ArgumentNullException(nameof(bot));

            if (string.IsNullOrEmpty(audience))
            {
                throw new ArgumentNullException(nameof(audience));
            }

            // The named pipe is local and so there is no network authentication to perform: so we can create the result here.
            var authenticationRequestResult = new AuthenticateRequestResult
            {
                Audience       = audience,
                ClaimsIdentity = appId != null?CreateClaimsIdentity(appId) : new ClaimsIdentity(),
                                     CallerId = callerId
            };

            // Tie the authentication results, the named pipe, the adapter and the bot together to be ready to handle any inbound activities
            var streamingActivityProcessor = new StreamingActivityProcessor(authenticationRequestResult, pipeName, this, bot);

            // Start receiving activities on the named pipe
            await streamingActivityProcessor.ListenAsync().ConfigureAwait(false);
        }
Beispiel #3
0
        /// <summary>
        /// Used to connect the adapter to a named pipe.
        /// </summary>
        /// <param name="pipeName">The name of the named pipe.</param>
        /// <param name="bot">The bot instance to use.</param>
        /// <param name="appId">The bot's application id.</param>
        /// <param name="audience">The audience to use for outbound communication. This will vary by cloud environment.</param>
        /// <param name="callerId">The callerId, this may be NULL.</param>
        /// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
        public async Task ConnectNamedPipeAsync(string pipeName, IBot bot, string appId, string audience, string callerId)
        {
            if (string.IsNullOrEmpty(pipeName))
            {
                throw new ArgumentNullException(nameof(pipeName));
            }

            _ = bot ?? throw new ArgumentNullException(nameof(bot));

            if (string.IsNullOrEmpty(audience))
            {
                throw new ArgumentNullException(nameof(audience));
            }

            // The named pipe is local and so there is no network authentication to perform: so we can create the result here.
            var authenticationRequestResult = new AuthenticateRequestResult
            {
                Audience       = audience,
                ClaimsIdentity = appId != null?CreateClaimsIdentity(appId) : new ClaimsIdentity(),
                                     CallerId = callerId
            };

            // Tie the authentication results, the named pipe, the adapter and the bot together to be ready to handle any inbound activities
            var connectionId = Guid.NewGuid();

            using (var scope = Logger.BeginScope(connectionId))
            {
#pragma warning disable CA2000 // Dispose objects before losing scope: StreamingRequestHandler is responsible for disposing StreamingConnection
                var connection = new NamedPipeStreamingConnection(pipeName, Logger);
#pragma warning restore CA2000 // Dispose objects before losing scope

                using (var streamingActivityProcessor = new StreamingActivityProcessor(authenticationRequestResult, connection, this, bot))
                {
                    // Start receiving activities on the named pipe
                    _streamingConnections.TryAdd(connectionId, streamingActivityProcessor);
                    Log.WebSocketConnectionStarted(Logger);
                    await streamingActivityProcessor.ListenAsync(CancellationToken.None).ConfigureAwait(false);

                    _streamingConnections.TryRemove(connectionId, out _);
                    Log.WebSocketConnectionCompleted(Logger);
                }
            }
        }
        private async Task ConnectAsync(HttpRequest httpRequest, IBot bot, CancellationToken cancellationToken)
        {
            Logger.LogInformation($"Received request for web socket connect.");

            // Grab the auth header from the inbound http request
            var authHeader = httpRequest.Headers["Authorization"];

            // Grab the channelId which should be in the http headers
            var channelIdHeader = httpRequest.Headers["channelid"];

            var authenticationRequestResult = await BotFrameworkAuthentication.AuthenticateStreamingRequestAsync(authHeader, channelIdHeader, cancellationToken).ConfigureAwait(false);

            // Transition the request to a WebSocket connection
            var socket = await httpRequest.HttpContext.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);

            // Tie the authentication results, the socket, the adapter and the bot together to be ready to handle any inbound activities
            var streamingActivityProcessor = new StreamingActivityProcessor(authenticationRequestResult, socket, this, bot);

            // Start receiving activities on the socket
            await streamingActivityProcessor.ListenAsync().ConfigureAwait(false);
        }