/// <summary> /// Constructs an IdServerSettings instance with the options specified in section named by configSection in the IConfiguration object. /// </summary> public static IdServerSettings GetIdServerSettings(this IConfiguration config, string configSection) { var idServerSettings = new IdServerSettings(); IConfiguration section = string.IsNullOrEmpty(configSection) ? config : config.GetSection(configSection); if (section == null || !section.GetChildren().Any()) { return(null); } idServerSettings.IdServerUrl = section["IdServerUrl"]; idServerSettings.IdServerClientId = section["IdServerClientId"]; idServerSettings.IdServerClientSecret = section["IdServerClientSecret"]; idServerSettings.Scope = section["Scope"]; idServerSettings.RequireHttps = section["RequireHttps"] == null ? false : Convert.ToBoolean(section["RequireHttps"]); return(idServerSettings); }
private IRestHttpClient Build(IdServerSettings settings = null) { if (settings == null) { settings = new IdServerSettings { IdServerUrl = "http://localhost:5400/ids", IdServerClientId = "IntTest", IdServerClientSecret = "SecretSquirrel", Scope = "MrsWorkflow.Api" }; } var serviceCollection = new ServiceCollection(); serviceCollection.ConfigureForClientCredentials(settings); var serviceProvider = serviceCollection.BuildServiceProvider(); return(serviceProvider.GetService <IRestHttpClient>()); }
/// <summary> /// Configures the container to return <see cref="IRestHttpClient"/> using ClientCredentialsTokenRequestAuthorization for authorization. /// <para>Note: this configures ClientCredentialsTokenRequestAuthorization as a singleton so don't use this if using <see cref="IRestHttpClient"/> /// against multiple API's with different authorization.</para> /// </summary> /// <param name="services"></param> /// <param name="idServerSettings"></param> /// <param name="retryIfUnauthorized"></param> /// <returns></returns> /// <remarks>ClientCredentialsTokenRequestAuthorization is configured as a singleton so that tokens are shared between instances of IRestHttpClient.</remarks> public static IServiceCollection ConfigureForClientCredentials(this IServiceCollection services, IdServerSettings idServerSettings, bool retryIfUnauthorized = true) { services.AddSingleton <IRequestAuthorization>(new ClientCredentialsTokenRequestAuthorization(idServerSettings)); services.AddTransient <IRestHttpClient, RestHttpClient>( serviceProvider => new RestHttpClient(serviceProvider.GetService <IRequestAuthorization>(), retryIfUnauthorized) ); return(services); }