async Task IAuthenticationProvider.AuthenticateRequestAsync(HttpRequestMessage request)
        {
            // look in the IBotDataBag for the token
            string objectIdentifier;
            string tenantID = null;
            byte[] tokenBlob = null;
            bool found
                = bag.TryGetValue(Keys.ObjectID, out objectIdentifier)
                && bag.TryGetValue(Keys.TenantID, out tenantID)
                && bag.TryGetValue(Keys.TokenCache, out tokenBlob);

            // if not found, then throw the exception that will restart the login flow
            if (! found)
            {
                throw new AdalSilentTokenAcquisitionException();
            }

            // deserialize the TokenCache and try to refresh the token silently
            var tokenCache = new TokenCache(tokenBlob);
            var token = await AcquireTokenSilentAsync(this.keys.ClientID, this.keys.ClientSecret, objectIdentifier, tenantID, tokenCache);

            // update the IBotDataBag with the new token if it's changed
            tokenBlob = tokenCache.Serialize();
            bag.SetValue(Keys.TokenCache, tokenBlob);

            // add the access token to the authentication header for the Microsoft Graph request
            var accessToken = token.AccessToken;
            request.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
        }
Exemplo n.º 2
0
        public void ClearCache()
        {
            string cache_filename = GetTokenCachePath();

            if (System.IO.File.Exists(cache_filename))
            {
                var bytes       = System.IO.File.ReadAllBytes(cache_filename);
                var token_cache = new MSAD.TokenCache(bytes);
                token_cache.Clear();
                System.IO.File.WriteAllBytes(cache_filename, token_cache.Serialize());
            }
        }
        public void ClearCache()
        {
            string cache_filename = GetTokenCachePath();

            if (System.IO.File.Exists(cache_filename))
            {
                var bytes       = System.IO.File.ReadAllBytes(cache_filename);
                var token_cache = new Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache(bytes);
                token_cache.Clear();
                System.IO.File.WriteAllBytes(cache_filename, token_cache.Serialize());
            }
        }
Exemplo n.º 4
0
        private static void SaveTokenCache(MSAD.TokenCache token_cache, string filename)
        {
            var bytes = token_cache.Serialize();

            System.IO.File.WriteAllBytes(filename, bytes);
        }