Class ApiKeySentinelMiddleware
- Namespace
- Cuemon.AspNetCore.Http.Headers
- Assembly
- Cuemon.AspNetCore.dll
Provides an API key sentinel middleware implementation for ASP.NET Core.
public class ApiKeySentinelMiddleware : ConfigurableMiddleware<ApiKeySentinelOptions>, IConfigurable<ApiKeySentinelOptions>
- Inheritance
-
ApiKeySentinelMiddleware
- Implements
- Inherited Members
Examples
The following example demonstrates how to register and use ApiKeySentinelMiddleware in the ASP.NET Core pipeline.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
namespace Cuemon.AspNetCore.Http.Headers;
public static class ApiKeySentinelMiddlewareExample
{
public static async Task DemonstrateAsync()
{
var context = new DefaultHttpContext();
var options = Options.Create(new ApiKeySentinelOptions
{
AllowedKeys = new List<string> { "secret-key" }
});
context.Request.Headers[options.Value.HeaderName] = "secret-key";
var middleware = new ApiKeySentinelMiddleware(
httpContext =>
{
httpContext.Response.StatusCode = StatusCodes.Status200OK;
return Task.CompletedTask;
},
options);
await middleware.InvokeAsync(context);
Console.WriteLine(context.Response.StatusCode);
}
}
Constructors
ApiKeySentinelMiddleware(RequestDelegate, IOptions<ApiKeySentinelOptions>)
Initializes a new instance of the ApiKeySentinelMiddleware class.
public ApiKeySentinelMiddleware(RequestDelegate next, IOptions<ApiKeySentinelOptions> setup)
Parameters
nextRequestDelegateThe delegate of the request pipeline to invoke.
setupIOptions<ApiKeySentinelOptions>The UserAgentSentinelOptions which need to be configured.
ApiKeySentinelMiddleware(RequestDelegate, Action<ApiKeySentinelOptions>)
Initializes a new instance of the ApiKeySentinelMiddleware class.
public ApiKeySentinelMiddleware(RequestDelegate next, Action<ApiKeySentinelOptions> setup)
Parameters
nextRequestDelegateThe delegate of the request pipeline to invoke.
setupAction<ApiKeySentinelOptions>The UserAgentSentinelOptions which need to be configured.
Methods
InvokeAsync(HttpContext)
Executes the UserAgentSentinelMiddleware.
public override Task InvokeAsync(HttpContext context)
Parameters
contextHttpContextThe context of the current request.
Returns
- Task
A task that represents the execution of this middleware.