Table of Contents

Class UserAgentSentinelOptions

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

Configuration options for UserAgentSentinelMiddleware and related.

public class UserAgentSentinelOptions : IValidatableParameterObject, IParameterObject
Inheritance
UserAgentSentinelOptions
Implements

Examples

The following example demonstrates how to configure to restrict which User-Agent headers are allowed.

using System;
using System.Collections.Generic;

        namespace Cuemon.AspNetCore.Http.Headers;

        public static class UserAgentSentinelOptionsExample
        {
            public static void Demonstrate()
            {
                var options = new UserAgentSentinelOptions
        {
            RequireUserAgentHeader = true,
            ValidateUserAgentHeader = true,
            AllowedUserAgents = new List<string> { "Cuemon-Agent" }
        };

        options.ValidateOptions();
        using var response = options.ResponseHandler("Unknown-Agent");
        var message = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
        Console.WriteLine(message);
            }
        }

Constructors

UserAgentSentinelOptions()

Initializes a new instance of the UserAgentSentinelOptions class.

public UserAgentSentinelOptions()

Remarks

The following table shows the initial property values for an instance of UserAgentSentinelOptions.

PropertyInitial Value
AllowedUserAgentsnew List{string}();
BadRequestMessageThe requirements of the request was not met.
ForbiddenMessageThe HTTP User-Agent specified was rejected.
RequireUserAgentHeaderfalse
ValidateUserAgentHeaderfalse
ResponseHandlerA HttpResponseMessage initialized to either a HTTP status code 400 or 403 and a body of either BadRequestMessage or ForbiddenMessage.
UseGenericResponsefalse

Properties

AllowedUserAgents

Gets or sets a list of whitelisted user agents.

public IList<string> AllowedUserAgents { get; set; }

Property Value

IList<string>

A list of whitelisted user agents.

BadRequestMessage

Gets or sets the message of a request missing the requirements of a User-Agent header.

public string BadRequestMessage { get; set; }

Property Value

string

The message of a request missing the requirements of a User-Agent header.

ForbiddenMessage

Gets or sets the message of a request without a valid User-Agent header.

public string ForbiddenMessage { get; set; }

Property Value

string

The message of a request without a valid User-Agent header.

RequireUserAgentHeader

Gets or sets a value indicating whether a HTTP User-Agent header must be present in the request.

public bool RequireUserAgentHeader { get; set; }

Property Value

bool

true if the HTTP User-Agent header must be present in the request; otherwise, false.

ResponseHandler

Gets or sets the function delegate that configures the response in the form of a HttpResponseMessage.

public Func<string, HttpResponseMessage> ResponseHandler { get; set; }

Property Value

Func<string, HttpResponseMessage>

The function delegate that configures the response in the form of a HttpResponseMessage.

UseGenericResponse

Gets or sets a value indicating whether the produced ResponseHandler should be as neutral as possible.

public bool UseGenericResponse { get; set; }

Property Value

bool

true if the produced ResponseHandler should be as neutral as possible; otherwise, false.

ValidateUserAgentHeader

Gets or sets a value indicating whether a HTTP User-Agent header must be validated against AllowedUserAgents.

public bool ValidateUserAgentHeader { get; set; }

Property Value

bool

true if the HTTP User-Agent header must be validated against AllowedUserAgents; otherwise, false.

Methods

ValidateOptions()

Determines whether the public read-write properties of this instance are in a valid state.

public void ValidateOptions()

Remarks

This method is expected to throw exceptions when one or more conditions fails to be in a valid state.

Exceptions

InvalidOperationException

ResponseHandler cannot be null - or - AllowedUserAgents cannot be null.