예제 #1
0
        protected override async Task <InvokeResponse> ProcessMessageRequestAsync(HttpRequestMessage request, BotFrameworkAdapter botFrameworkAdapter, Func <ITurnContext, Task> botCallbackHandler, CancellationToken cancellationToken)
        {
            const string BotAppIdHttpHeaderName           = "MS-BotFramework-BotAppId";
            const string BotAppIdQueryStringParameterName = "BotAppId";

            var botAppId = default(string);

            if (request.Headers.TryGetValues(BotAppIdHttpHeaderName, out var botIdHeaders))
            {
                botAppId = botIdHeaders.FirstOrDefault();
            }
            else
            {
                botAppId = request.GetQueryNameValuePairs()
                           .Where(kvp => kvp.Key == BotAppIdQueryStringParameterName)
                           .Select(kvp => kvp.Value)
                           .FirstOrDefault();
            }

            if (string.IsNullOrEmpty(botAppId))
            {
                throw new InvalidOperationException($"Expected a Bot App ID in a header named \"{BotAppIdHttpHeaderName}\" or in a querystring parameter named \"{BotAppIdQueryStringParameterName}\".");
            }

            var conversationReference = await request.Content.ReadAsAsync <ConversationReference>(BotMessageHandlerBase.BotMessageMediaTypeFormatters, cancellationToken);

            await botFrameworkAdapter.ContinueConversation(botAppId, conversationReference, botCallbackHandler, cancellationToken);

            return(null);
        }
예제 #2
0
        protected override async Task <InvokeResponse> ProcessMessageRequestAsync(HttpRequest request, BotFrameworkAdapter botFrameworkAdapter, Func <ITurnContext, Task> botCallbackHandler)
        {
            const string BotAppIdHttpHeaderName        = "MS-BotFramework-BotAppId";
            const string BotIdQueryStringParameterName = "BotAppId";

            if (!request.Headers.TryGetValue(BotAppIdHttpHeaderName, out var botAppIdHeaders))
            {
                if (!request.Query.TryGetValue(BotIdQueryStringParameterName, out botAppIdHeaders))
                {
                    throw new InvalidOperationException($"Expected a Bot App ID in a header named \"{BotAppIdHttpHeaderName}\" or in a querystring parameter named \"{BotIdQueryStringParameterName}\".");
                }
            }

            var botAppId = botAppIdHeaders.First();
            var conversationReference = default(ConversationReference);

            using (var bodyReader = new JsonTextReader(new StreamReader(request.Body, Encoding.UTF8)))
            {
                conversationReference = BotMessageHandlerBase.BotMessageSerializer.Deserialize <ConversationReference>(bodyReader);
            }

            await botFrameworkAdapter.ContinueConversation(botAppId, conversationReference, botCallbackHandler);

            return(null);
        }
        protected override async Task <InvokeResponse> ProcessMessageRequestAsync(HttpRequestMessage request, BotFrameworkAdapter botFrameworkAdapter, Func <ITurnContext, Task> botCallbackHandler, CancellationToken cancellationToken)
        {
            var conversationReference = await request.Content.ReadAsAsync <ConversationReference>(BotMessageHandlerBase.BotMessageMediaTypeFormatters, cancellationToken);

            await botFrameworkAdapter.ContinueConversation(conversationReference, botCallbackHandler);

            return(null);
        }
예제 #4
0
        protected override async Task ProcessMessageRequestAsync(HttpRequest request, BotFrameworkAdapter botFrameworkAdapter, Func <IBotContext, Task> botCallbackHandler)
        {
            var conversationReference = default(ConversationReference);

            using (var bodyReader = new JsonTextReader(new StreamReader(request.Body, Encoding.UTF8)))
            {
                conversationReference = BotMessageHandlerBase.BotMessageSerializer.Deserialize <ConversationReference>(bodyReader);
            }

            await botFrameworkAdapter.ContinueConversation(conversationReference, botCallbackHandler);
        }
예제 #5
0
        public async Task <HttpResponseMessage> VSTSAuth([FromQuery] string code, [FromQuery] string state)
        {
            var queryParams = state;
            var cfbytes     = WebEncoders.Base64UrlDecode(queryParams);
            var _cf         = JsonConvert.DeserializeObject <ConversationReference>(System.Text.Encoding.UTF8.GetString(cfbytes));

            TokenModel token = new TokenModel();
            String     error = null;

            if (!String.IsNullOrEmpty(code))
            {
                error = PerformTokenRequest(GenerateRequestPostData(code), out token);
                if (!String.IsNullOrEmpty(error))
                {
                    Debug.WriteLine(error);
                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                }
            }

            CosmosDbHelper cdh = new CosmosDbHelper("VSTSDb", "VSTSToken");

            BotAdapter ba = new BotFrameworkAdapter(Environment.GetEnvironmentVariable("MicrosoftAppId"), Environment.GetEnvironmentVariable("MicrosoftAppPassword"));

            await ba.ContinueConversation(Environment.GetEnvironmentVariable("MicrosoftAppId"), _cf, async (context) =>
            {
                // Write token to the database associated with a specific user
                var usertoken = new
                {
                    userId = context.Activity.From.Id,
                    token
                };

                try
                {
                    var document = await cdh.docClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("VSTSDb", "VSTSToken"), usertoken);
                }
                catch (Exception ex)
                {
                    // TODO: More logic for what to do on a failed write can be added here
                    await context.SendActivity("exception in storing token");
                    throw ex;
                }

                await context.SendActivity("You are logged in VSTS!");
            });


            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        public async Task <HttpResponseMessage> OAuthCallback(
            [FromQuery] string code,
            [FromQuery] string state,
            CancellationToken cancellationToken)
        {
            try
            {
                var    queryParams = state;
                object tokenCache  = new Microsoft.Identity.Client.TokenCache();

                var cfbytes = WebEncoders.Base64UrlDecode(queryParams);
                var _cf     = JsonConvert.DeserializeObject <ConversationReference>(System.Text.Encoding.UTF8.GetString(cfbytes));

                // Exchange the Auth code with Access token
                var token = await AuthenticationHelper.GetTokenByAuthCodeAsync(code);

                // create the context from the ConversationReference
                // this require also the storage to store context.state
                CloudStorageAccount csa     = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("TABLE_CONNSTRING"));
                IStorage            storage = new AzureTableStorage(csa, Environment.GetEnvironmentVariable("tablename"));

                BotFrameworkAdapter bot = new BotFrameworkAdapter(Environment.GetEnvironmentVariable("MicrosoftAppId"), Environment.GetEnvironmentVariable("MicrosoftAppPassword"));
                bot.Use(new UserStateManagerMiddleware(storage));
                await bot.ContinueConversation(_cf, async (IBotContext context) =>
                {
                    // store the acces token in the BotState
                    context.State.UserProperties["token"] = token;
                    //send logged in message using context created
                    context.Reply("You are now logged in");
                });

                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                // Callback is called with no pending message as a result the login flow cannot be resumed.
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
        }