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 OnTurnAsync(ITurnContext context, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken))
        {
            var authToken = TokenStorage.LoadConfiguration(context.Activity.Conversation.Id);

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

                    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.SendActivityAsync(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.TurnState.Add(AUTH_TOKEN_KEY, authToken);
                    await next(cancellationToken);
                }
            }
            else
            {
                // make the authtoken available to downstream pipeline components
                context.TurnState.Add(AUTH_TOKEN_KEY, authToken);
                await next(cancellationToken);
            }
        }
 public void SaveConfiguration(ConversationAuthToken state)
 {
     InMemoryDictionary[state.Id] = state;
 }