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
        //public static async Task<AuthenticationResult> GetTokenByAuthCodeAsync(string authorizationCode, AuthenticationSettings authenticationSettings, string[] scopes)
        //{
        //    var tokenCache = TokenCacheFactory.GetMSALTokenCache();
        //    Microsoft.Identity.Client.ConfidentialClientApplication client = new Microsoft.Identity.Client.ConfidentialClientApplication(authenticationSettings.ClientId, authenticationSettings.RedirectUrl, new Microsoft.Identity.Client.ClientCredential(authenticationSettings.ClientSecret), tokenCache);
        //    Uri redirectUri = new Uri(authenticationSettings.RedirectUrl);
        //    var result = await client.AcquireTokenByAuthorizationCodeAsync(scopes, authorizationCode);
        //    AuthenticationResult authResult = ConvertAuthenticationResult(result, tokenCache);
        //    return authResult;
        //}

        public static async Task <AuthenticationResult> GetToken(string userUniqueId, AuthenticationSettings authenticationSettings, string resourceId)
        {
            var tokenCache = TokenCacheFactory.GetADALTokenCache();

            Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authenticationSettings.EndpointUrl + "/" + authenticationSettings.Tenant, tokenCache);
            var result = await context.AcquireTokenSilentAsync(resourceId, new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(authenticationSettings.ClientId, authenticationSettings.ClientSecret), new Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier(userUniqueId, Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType.UniqueId));

            AuthenticationResult authResult = ConvertAuthenticationResult(result, tokenCache);

            return(authResult);
        }
示例#3
0
        public static async Task <AuthenticationResult> GetTokenByAuthCodeAsync(string authorizationCode, AuthenticationSettings authenticationSettings)
        {
            var tokenCache = TokenCacheFactory.GetADALTokenCache();

            Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authenticationSettings.EndpointUrl + "/" + authenticationSettings.Tenant, tokenCache);
            Uri redirectUri = new Uri(authenticationSettings.RedirectUrl);
            var result      = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, redirectUri, new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(authenticationSettings.ClientId, authenticationSettings.ClientSecret));

            AuthenticationResult authResult = ConvertAuthenticationResult(result, tokenCache);

            return(authResult);
        }