Table of Contents

Class UserAgentSentinelMiddleware

Namespace
Cuemon.AspNetCore.Http.Headers
Assembly
Cuemon.AspNetCore.dll

Provides a HTTP User-Agent sentinel middleware implementation for ASP.NET Core.

public class UserAgentSentinelMiddleware : ConfigurableMiddleware<UserAgentSentinelOptions>, IConfigurable<UserAgentSentinelOptions>
Inheritance
UserAgentSentinelMiddleware
Implements
Inherited Members

Examples

The following example demonstrates how to register the to require a specific User-Agent header on incoming requests.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;

        namespace Cuemon.AspNetCore.Http.Headers;

        public static class UserAgentSentinelMiddlewareExample
        {
            public static async Task DemonstrateAsync()
            {
                var context = new DefaultHttpContext();
        var options = Options.Create(new UserAgentSentinelOptions
        {
            RequireUserAgentHeader = true,
            ValidateUserAgentHeader = true,
            AllowedUserAgents = new List<string> { "Cuemon-Agent" }
        });

        context.Request.Headers[HeaderNames.UserAgent] = "Cuemon-Agent";

        var middleware = new UserAgentSentinelMiddleware(
            httpContext =>
            {
                httpContext.Response.StatusCode = StatusCodes.Status200OK;
                return Task.CompletedTask;
            },
            options);

        await middleware.InvokeAsync(context);
        Console.WriteLine(context.Response.StatusCode);
            }
        }

Constructors

UserAgentSentinelMiddleware(RequestDelegate, IOptions<UserAgentSentinelOptions>)

Initializes a new instance of the UserAgentSentinelMiddleware class.

public UserAgentSentinelMiddleware(RequestDelegate next, IOptions<UserAgentSentinelOptions> setup)

Parameters

next RequestDelegate

The delegate of the request pipeline to invoke.

setup IOptions<UserAgentSentinelOptions>

The UserAgentSentinelOptions which need to be configured.

UserAgentSentinelMiddleware(RequestDelegate, Action<UserAgentSentinelOptions>)

Initializes a new instance of the UserAgentSentinelMiddleware class.

public UserAgentSentinelMiddleware(RequestDelegate next, Action<UserAgentSentinelOptions> setup)

Parameters

next RequestDelegate

The delegate of the request pipeline to invoke.

setup Action<UserAgentSentinelOptions>

The UserAgentSentinelOptions which need to be configured.

Methods

InvokeAsync(HttpContext)

public override Task InvokeAsync(HttpContext context)

Parameters

context HttpContext

The context of the current request.

Returns

Task

A task that represents the execution of this middleware.