Beispiel #1
0
        /// <summary>
        /// All auth types except JWTBearer
        /// Route Parameters
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="configUrlVar">Name of Host Environment Variable holding the Configuration URL</param>
        /// <param name="authType">Authentication type: Windows, Certificate, ApiKey, or Anon (default: Windows)</param>
        /// <param name="authSecretVar">Certificate Thumbprint, or APIKey - not needed for Windows</param>
        /// <param name="rParams">Route paramters to be added to the ConfigURL. When no Query or Route parameters are present the default route parameter will be applied: {"AssemblyName"}</param>
        /// <param name="optional">Prevent program from loading if the APIProvider does not build successfully (default: 'false')</param>

        public ApiClientSource(IConfigurationBuilder builder, string configUrlVar, string authType = null, string authSecretVar = null, string[] rParams = null, bool optional = false)
        {
            _optional = optional;
            try
            {
                //Create the apiOptions object
                ApiSourceOptions apiOptions = new ApiSourceOptions(configUrlVar, authType, authSecretVar, rParams, _optional);

                //Initialize the correct HTTP client for the Authentication type
                _client  = HttpClientHelper.GetHttpClient(apiOptions);
                _request = HttpClientHelper.GetHttpRequest(apiOptions);
            }
            catch (Exception e)
            {
                if (!optional)
                {
                    throw e;
                }
                else
                {
                    _apiClientSourceError = e.Message;
                }
                return;
            }
        }
        //This method is used to add the API Key header using a request
        public static HttpRequestMessage GetHttpRequest(ApiSourceOptions apiOptions)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, apiOptions.ConfigUrl);

            switch (apiOptions.AuthType)
            {
            case "ApiKey":
                request.Headers.Add("X-API-Key", apiOptions.AuthSecret);
                break;

            case "Windows":
            case "Certificate":
            case "Anon":
            case "Bearer":
                break;
            }

            return(request);
        }
Beispiel #3
0
 // JwtBearer Authentication with Route Params
 public ApiClientSource(IConfigurationBuilder builder, string configUrlVar, string authVar, string clientIdVar, string clientSecretVar, string clientScopeVar, string[] routeParams = null, bool optional = false)
 {
     _optional = optional;
     try
     {
         ApiSourceOptions apiOptions = new ApiSourceOptions(configUrlVar, authVar, clientIdVar, clientSecretVar, clientScopeVar, routeParams, optional);
         //Initialize the correct HTTP client for the Authentication type
         _client  = HttpClientHelper.GetHttpClient(apiOptions);
         _request = HttpClientHelper.GetHttpRequest(apiOptions);
     }
     catch (Exception e)
     {
         if (!optional)
         {
             throw e;
         }
         else
         {
             _apiClientSourceError = e.Message;
         }
     }
 }
Beispiel #4
0
 //Configuration Object Parameter - supports all authentication and parameter types - Requires configuration section ConfigOptions:ApiSource
 public ApiClientSource(IConfigurationBuilder builder, IConfiguration config, bool optional = false)
 {
     _optional = optional;
     try
     {
         //Create the apiOptions object
         ApiSourceOptions apiOptions = new ApiSourceOptions(config, optional);
         //Initialize the correct HTTP client for the Authentication type
         _client  = HttpClientHelper.GetHttpClient(apiOptions);
         _request = HttpClientHelper.GetHttpRequest(apiOptions);
     }
     catch (Exception e)
     {
         if (!optional)
         {
             throw e;
         }
         else
         {
             _apiClientSourceError = e.Message;
         }
     }
 }
        // This method is used when a client is needed, with or without handler. Handles all auth types except API key
        public static HttpClient GetHttpClient(ApiSourceOptions options)
        {
            var               baseAddress  = new Uri($"{options.ConfigUrl}");
            HttpClient        returnClient = null;
            HttpClientHandler handler      = null;

            switch (options.AuthType)
            {
            case "Certificate":
                X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                certStore.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, options.AuthSecret, false);
                if (certCollection.Count > 0)
                {
                    X509Certificate2 cert = certCollection[0];
                    handler = new HttpClientHandler();
                    handler.ClientCertificates.Add(cert);
                }
                else
                {
                    throw new Exception($"Certificate not found for thumbprint: {options.AuthSecret}");
                }


                returnClient = new HttpClient(handler)
                {
                    BaseAddress = baseAddress
                };
                break;

            case "ApiKey":
                returnClient = new HttpClient()
                {
                    BaseAddress = baseAddress
                };
                break;

            case "JwtBearer":
                returnClient = new HttpClient()
                {
                    BaseAddress = baseAddress
                };
                returnClient.SetBearerToken(GetBearerToken(options.JWTBearerOptions));

                break;

            case "Windows":
                handler = new HttpClientHandler
                {
                    Credentials = CredentialCache.DefaultNetworkCredentials
                };
                returnClient = new HttpClient(handler)
                {
                    BaseAddress = baseAddress
                };
                break;

            case "Anon":
            default:

                returnClient = new HttpClient()
                {
                    BaseAddress = baseAddress
                };
                break;
            }

            return(returnClient);
        }