예제 #1
0
 /// <summary>
 /// Creates an IApiClient.
 /// </summary>
 /// <param name="settings"></param>
 /// <returns></returns>
 public IApiClient CreateApiClient(ApiClientFactorySettings settings)
 {
     ValidationHelper.ArgumentNotNull(settings, nameof(settings));
     ValidationHelper.ArgumentNotNullOrWhiteSpace(settings.ConnectorApiUrl, nameof(settings.ConnectorApiUrl));
     // get the api client generated by AutoRest from the swagger as a singleton object
     return(GetApiClient(settings));
 }
예제 #2
0
        /// <summary>
        /// Acquires an OAuth 2.0 access token from Azure AD for use with the Records365 Connector API.
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="useTokenCache"></param>
        /// <returns></returns>
        public async Task <AuthenticationResult> AcquireTokenAsync(AuthenticationHelperSettings settings, bool useTokenCache = true)
        {
            ValidationHelper.ArgumentNotNull(settings, nameof(settings));
            ValidationHelper.ArgumentNotNullOrWhiteSpace(settings.AuthenticationResource, nameof(settings.AuthenticationResource));
            ValidationHelper.ArgumentNotNullOrWhiteSpace(settings.ClientId, nameof(settings.ClientId));

            var authority = GetAuthority(settings);

            // sign in & get an authentication token...
            var authenticationContext = useTokenCache
                                        // by default an internal token cache is used
                ? new AuthenticationContext(authority)
                                        // pass a null token cache so that the token must be retrieved from the authority
                : new AuthenticationContext(authority, null);


#if NETSTANDARD2_0
            //NOTE: The following call may throw a PlatformNotSupported exception that occurs the first time a token is acquired.
            //However, the exception will not raise beyond this point, and the call will automatically retry and resolved a token on the second attempt.
            //This is due to the first (throwing) attempt actually acquiring a token, storing it in the AuthenticationContext token cache, and then throwing an exception regardless.
            //Later attempts simply use the cached token and thus avoid the issue. While undocumented, it is believed that this is the intended functionality.
            var aadAuthenticationResult = await authenticationContext.AcquireTokenAsync(settings.AuthenticationResource, new ClientCredential(settings.ClientId, SecureStringToString(settings.ClientSecret))).ConfigureAwait(false);
#else
            var aadAuthenticationResult = await authenticationContext.AcquireTokenAsync(settings.AuthenticationResource, new ClientCredential(settings.ClientId, new SecureClientSecret(settings.ClientSecret))).ConfigureAwait(false);
#endif
            return(new AuthenticationResult
            {
                AccessTokenType = aadAuthenticationResult.AccessTokenType,
                AccessToken = aadAuthenticationResult.AccessToken
            });
        }
        /// <summary>
        /// Default blob factory used for Production workloads
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static ICloudBlob DefaultBlobFactory(string url)
        {
            ValidationHelper.ArgumentNotNullOrWhiteSpace(url, nameof(url));
            //Example of CloudBlockBlob with a SaS token: https://docs.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1
            var blockBlob = new CloudBlockBlob(new Uri(url));

            return(blockBlob);
        }
예제 #4
0
 private string GetAuthority(AuthenticationHelperSettings settings)
 {
     ValidationHelper.ArgumentNotNullOrWhiteSpace(settings.TenantDomainName, nameof(settings.TenantDomainName));
     return(AuthEndpointPrefix + settings.TenantDomainName);
 }