Table of Contents

ApiKeySentinelMiddleware Class

Definition

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.

// Program.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Cuemon.AspNetCore.Http.Headers;

await ApiKeySentinelMiddlewareExample.DemonstrateAsync();

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.