コード例 #1
0
        public static async Task <string> GetADALAccessToken(this IBotContext context, string resource)
        {
            AuthenticationSettings authenticationSettings = AuthenticationSettings.GetFromAppSettings();
            AuthenticationResult   authenticationResult;

            string authenticationKey = AuthenticationConstants.AuthDialogId_AzureAD + '_' + AuthenticationConstants.AuthResultKey;

            if (context.UserData.TryGetValue(authenticationKey, out authenticationResult))
            {
                try
                {
                    var tokenCache = TokenCacheFactory.SetADALTokenCache(authenticationResult.TokenCache);

                    var result = await AzureADHelper.GetToken(authenticationResult.UserUniqueId, authenticationSettings, resource);

                    authenticationResult.AccessToken       = result.AccessToken;
                    authenticationResult.ExpiresOnUtcTicks = result.ExpiresOnUtcTicks;
                    authenticationResult.TokenCache        = tokenCache.Serialize();
                    context.StoreAuthResult(authenticationResult);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Failed to renew token: " + ex.Message);
                    await context.PostAsync("Your credentials expired and could not be renewed automatically!");

                    await context.Logout(authenticationSettings);

                    return(null);
                }
                return(authenticationResult.AccessToken);
            }
            return(null);
        }
コード例 #2
0
        protected override async Task <Auth.AuthenticationResult> GetTokenByAuthCodeAsync(NameValueCollection parameters)
        {
            string code = parameters["code"];

            AuthenticationSettings authenticationSettings = AuthenticationSettings.GetFromAppSettings();
            // Exchange the Auth code with Access token
            var token = await AzureADHelper.GetTokenByAuthCodeAsync(code, authenticationSettings);

            return(token);
        }
コード例 #3
0
 public override Task <string> GetAuthUrl(ResumptionCookie resumptionCookie)
 {
     return(AzureADHelper.GetAuthUrlAsync(resumptionCookie, this.resourceId));
 }