Table of Contents

UserAgentSentinelMiddleware Class

Definition

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

Name Description
UserAgentSentinelMiddleware(RequestDelegate, IOptions<UserAgentSentinelOptions>)

Initializes a new instance of the UserAgentSentinelMiddleware class.

UserAgentSentinelMiddleware(RequestDelegate, Action<UserAgentSentinelOptions>)

Initializes a new instance of the UserAgentSentinelMiddleware class.

Methods

Name Description
InvokeAsync(HttpContext)

Executes the UserAgentSentinelMiddleware.