Exemple #1
0
        public void SaveConfiguration(ConversationAuthToken state)
        {
            state.Id = "singletonkey";

            Dictionary <string, ConversationAuthToken> values = File.Exists("accessTokens.json") ? JsonConvert.DeserializeObject <Dictionary <string, ConversationAuthToken> >(File.ReadAllText("accessTokens.json")) : new Dictionary <string, ConversationAuthToken>();

            values[state.Id] = state;

            File.WriteAllText("accessTokens.json", JsonConvert.SerializeObject(values));
        }
        public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next)
        {
            var authToken = TokenStorage.LoadConfiguration(context.Activity.Conversation.Id);

            if (authToken == null)
            {
                if (context.Activity.UserHasJustSentMessage() || context.Activity.UserHasJustJoinedConversation())
                {
                    var conversationReference = TurnContext.GetConversationReference(context.Activity);

                    var serializedCookie = WebUtility.UrlEncode(JsonConvert.SerializeObject(conversationReference));

                    var signInUrl = AzureAdExtensions.GetUserConsentLoginUrl(AzureAdTenant, AppClientId, AppRedirectUri, PermissionsRequested, serializedCookie);

                    var activity = context.Activity.CreateReply();
                    activity.AddSignInCard(signInUrl);

                    await context.SendActivity(activity);
                }
            }
            else if (authToken.ExpiresIn < DateTime.Now.AddMinutes(-10))
            {
                if (context.Activity.UserHasJustSentMessage() || context.Activity.UserHasJustJoinedConversation())
                {
                    var client      = new HttpClient();
                    var accessToken = await AzureAdExtensions.GetAccessTokenUsingRefreshToken(client, AzureAdTenant, authToken.RefreshToken, AppClientId, AppRedirectUri, AppClientSecret, PermissionsRequested);

                    // have to save it
                    authToken = new ConversationAuthToken(context.Activity.Conversation.Id)
                    {
                        AccessToken  = accessToken.accessToken,
                        RefreshToken = accessToken.refreshToken,
                        ExpiresIn    = accessToken.refreshTokenExpiresIn
                    };
                    TokenStorage.SaveConfiguration(authToken);

                    // make the authtoken available to downstream pipeline components
                    context.Services.Add(AUTH_TOKEN_KEY, authToken);
                    await next();
                }
            }
            else
            {
                // make the authtoken available to downstream pipeline components
                context.Services.Add(AUTH_TOKEN_KEY, authToken);
                await next();
            }
        }
Exemple #3
0
 public void SaveConfiguration(ConversationAuthToken state)
 {
     InMemoryDictionary[state.Id] = state;
 }