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
nextRequestDelegateThe delegate of the request pipeline to invoke.
setupIOptions<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
nextRequestDelegateThe delegate of the request pipeline to invoke.
setupAction<UserAgentSentinelOptions>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.