Ejemplo n.º 1
0
        /// <summary>
        /// For testing purposes only: allows testing token expiration.
        /// </summary>
        /// <param name="domain">The active directory domain or tenantId to authenticate with.</param>
        /// <param name="clientId">The active directory clientId for the application.</param>
        /// <param name="authenticationProvider">A source for the secure secret for this application.</param>
        /// <param name="settings">The active directory service side settings, including authority and token audience.</param>
        /// <param name="cache">The token cache to target during authentication.</param>
        /// <param name="expiration">The token expiration.</param>
        /// <returns>A ServiceClientCredentials object that can authenticate http requests as the given application.</returns>
        internal static async Task <ServiceClientCredentials> LoginSilentAsync(string domain, string clientId,
                                                                               IApplicationAuthenticationProvider authenticationProvider, ActiveDirectoryServiceSettings settings, TokenCache cache, DateTimeOffset expiration)
        {
            var audience   = settings.TokenAudience.ToString();
            var context    = GetAuthenticationContext(domain, settings, cache);
            var authResult = await authenticationProvider.AuthenticateAsync(clientId, audience, context);

            return(new TokenCredentials(new ApplicationTokenProvider(context, audience, clientId,
                                                                     authenticationProvider, authResult, expiration)));
        }
        /// <summary>
        /// Creates ServiceClientCredentials for authenticating requests as an active directory application.
        /// See <see href="https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-dotnet/">Active Directory Quickstart for .Net</see>
        /// for detailed instructions on creating an Azure Active Directory application.
        /// </summary>
        /// <param name="domain">The active directory domain or tenantId to authenticate with.</param>
        /// <param name="clientId">The active directory clientId for the application.</param>
        /// <param name="authenticationProvider">A source for the secure secret for this application.</param>
        /// <param name="settings">The active directory service side settings, including authority and token audience.</param>
        /// <param name="cache">The token cache to target during authentication.</param>
        /// <returns>A ServiceClientCredentials object that can authenticate http requests as the given application.</returns>
        public static async Task <ServiceClientCredentials> LoginSilentAsync(string domain, string clientId,
                                                                             IApplicationAuthenticationProvider authenticationProvider, ActiveDirectoryServiceSettings settings, TokenCache cache)
        {
            var audience   = settings.TokenAudience.OriginalString;
            var context    = GetAuthenticationContext(domain, settings, cache);
            var authResult = await authenticationProvider.AuthenticateAsync(clientId, audience, context);

            return(new TokenCredentials(
                       new ApplicationTokenProvider(context, audience, clientId, authenticationProvider, authResult),
                       authResult.TenantId,
                       authResult.UserInfo == null ? null : authResult.UserInfo.DisplayableId));
        }
        /// <summary>
        /// Gets an access token from the identity endpoint.
        /// Attempts to refresh the access token if it has expired.
        /// </summary>
        public virtual async Task <AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken)
        {
            try
            {
                AccessTokenResponse response;
                if (AccessTokenExpired)
                {
                    response = await _authenticationProvider.AuthenticateAsync(_tokenAudience, _scopes).ConfigureAwait(false);

                    _accessToken = response.AccessToken;
                    _expiration  = response.ExpiresOn.GetValueOrDefault();
                }

                return(new AuthenticationHeaderValue("Bearer", _accessToken));
            }
            catch (Exception authenticationException)
            {
                throw new AuthenticationException("Could not acquire access token.", authenticationException);
            }
        }
 /// <summary>
 /// For testing purposes only: allows testing token expiration.
 /// </summary>
 /// <param name="domain">The active directory domain or tenantId to authenticate with.</param>
 /// <param name="clientId">The active directory clientId for the application.</param>
 /// <param name="authenticationProvider">A source for the secure secret for this application.</param>
 /// <param name="settings">The active directory service side settings, including authority and token audience.</param>
 /// <param name="cache">The token cache to target during authentication.</param>
 /// <param name="expiration">The token expiration.</param>
 /// <returns>A ServiceClientCredentials object that can authenticate http requests as the given application.</returns>
 internal static async Task<ServiceClientCredentials> LoginSilentAsync(string domain, string clientId,
     IApplicationAuthenticationProvider authenticationProvider, ActiveDirectoryServiceSettings settings, TokenCache cache, DateTimeOffset expiration)
 {
     var audience = settings.TokenAudience.ToString();
     var context = GetAuthenticationContext(domain, settings, cache);
     var authResult = await authenticationProvider.AuthenticateAsync(clientId, audience, context);
     return new TokenCredentials(new ApplicationTokenProvider(context, audience, clientId,
             authenticationProvider, authResult, expiration));
 }
 /// <summary>
 /// Creates ServiceClientCredentials for authenticating requests as an active directory application.
 /// See <see href="https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-dotnet/">Active Directory Quickstart for .Net</see> 
 /// for detailed instructions on creating an Azure Active Directory application.
 /// </summary>
 /// <param name="domain">The active directory domain or tenantId to authenticate with.</param>
 /// <param name="clientId">The active directory clientId for the application.</param>
 /// <param name="authenticationProvider">A source for the secure secret for this application.</param>
 /// <param name="settings">The active directory service side settings, including authority and token audience.</param>
 /// <param name="cache">The token cache to target during authentication.</param>
 /// <returns>A ServiceClientCredentials object that can authenticate http requests as the given application.</returns>
 public static async Task<ServiceClientCredentials> LoginSilentAsync(string domain, string clientId,
     IApplicationAuthenticationProvider authenticationProvider, ActiveDirectoryServiceSettings settings, TokenCache cache)
 {
     var audience = settings.TokenAudience.ToString();
     var context = GetAuthenticationContext(domain, settings, cache);
     var authResult = await authenticationProvider.AuthenticateAsync(clientId, audience, context);
     return new TokenCredentials(
         new ApplicationTokenProvider(context, audience, clientId,authenticationProvider, authResult),
         authResult.TenantId,
         authResult.UserInfo == null ? null : authResult.UserInfo.DisplayableId);
 }