ApiKeySentinelMiddleware Class
Definition
- Namespace
- Cuemon.AspNetCore.Http.Headers
- Assemblies
- 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
| Name | Description |
|---|---|
| ApiKeySentinelMiddleware(RequestDelegate, IOptions<ApiKeySentinelOptions>) | Initializes a new instance of the ApiKeySentinelMiddleware class. |
| ApiKeySentinelMiddleware(RequestDelegate, Action<ApiKeySentinelOptions>) | Initializes a new instance of the ApiKeySentinelMiddleware class. |
Methods
| Name | Description |
|---|---|
| InvokeAsync(HttpContext) | Executes the UserAgentSentinelMiddleware. |