Class ServiceCollectionExtensions
- Namespace
- Cuemon.Extensions.AspNetCore.Http.Headers
- Assembly
- Cuemon.Extensions.AspNetCore.dll
Extension methods for the IServiceCollection interface.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
Examples
The following example demonstrates how to register API key and User-Agent validation rules and then inspect the resolved options through IOptions<T>.
using System;
using Cuemon.AspNetCore.Http.Headers;
using Cuemon.Extensions.AspNetCore.Http.Headers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace DocfxExamples;
public class HeaderServiceCollectionExtensionsExample
{
public static void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddApiKeySentinelOptions(options =>
{
options.AllowedKeys.Add("known-key");
options.HeaderName = "X-Test-Key";
options.UseGenericResponse = true;
});
services.AddUserAgentSentinelOptions(options =>
{
options.AllowedUserAgents.Add("Cuemon-Agent");
options.RequireUserAgentHeader = true;
options.ValidateUserAgentHeader = true;
});
var provider = services.BuildServiceProvider();
var apiKeyOptions = provider.GetRequiredService<IOptions<ApiKeySentinelOptions>>().Value;
var userAgentOptions = provider.GetRequiredService<IOptions<UserAgentSentinelOptions>>().Value;
Console.WriteLine($"{apiKeyOptions.HeaderName}:{apiKeyOptions.AllowedKeys.Count}");
Console.WriteLine($"{userAgentOptions.RequireUserAgentHeader}:{userAgentOptions.AllowedUserAgents.Count}");
}
}
Methods
AddApiKeySentinelOptions(IServiceCollection, Action<ApiKeySentinelOptions>)
Adds configuration of ApiKeySentinelOptions for the application.
public static IServiceCollection AddApiKeySentinelOptions(this IServiceCollection services, Action<ApiKeySentinelOptions> setup = null)
Parameters
servicesIServiceCollectionThe IServiceCollection to extend.
setupAction<ApiKeySentinelOptions>The ApiKeySentinelOptions which may be configured.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
Exceptions
- ArgumentNullException
servicescannot be null.- ArgumentException
setupfailed to configure an instance of ApiKeySentinelOptions in a valid state.
AddUserAgentSentinelOptions(IServiceCollection, Action<UserAgentSentinelOptions>)
Adds configuration of UserAgentSentinelOptions for the application.
public static IServiceCollection AddUserAgentSentinelOptions(this IServiceCollection services, Action<UserAgentSentinelOptions> setup = null)
Parameters
servicesIServiceCollectionThe IServiceCollection to extend.
setupAction<UserAgentSentinelOptions>The UserAgentSentinelOptions which may be configured.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
Exceptions
- ArgumentNullException
servicescannot be null.- ArgumentException
setupfailed to configure an instance of UserAgentSentinelOptions in a valid state.