Table of Contents

Class HttpContextDecoratorExtensions

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

Extension methods for the HttpContext class hidden behind the IDecorator<T> interface. This API supports the product infrastructure and is not intended to be used directly from your code.

public static class HttpContextDecoratorExtensions
Inheritance
HttpContextDecoratorExtensions

Examples

HttpContextDecoratorExtensions provides extension methods on Decorator.Enclose for invoking throttling sentinels, API key sentinels, user-agent sentinels, and writing exception descriptor responses on HttpContext. This example configures a DefaultHttpContext with "X-Api-Key: secret-key" and "User-Agent: Cuemon Docs" headers, sets up ThrottlingSentinelOptions with a quota of 2 requests per minute, ApiKeySentinelOptions with allowed keys, and UserAgentSentinelOptions. Key steps include calling InvokeThrottlerSentinelAsync, InvokeApiKeySentinelAsync, and InvokeUserAgentSentinelAsync, then writing a 400 Bad Request exception descriptor response via WriteExceptionDescriptorResponseAsync. Console output confirms the final response status code.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Cuemon;
using Cuemon.AspNetCore.Diagnostics;
using Cuemon.AspNetCore.Http.Headers;
using Cuemon.AspNetCore.Http.Throttling;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
using HttpMediaTypeHeaderValue = System.Net.Http.Headers.MediaTypeHeaderValue;

namespace Cuemon.AspNetCore.Http;

public static class HttpContextDecoratorExtensionsExample
{
    public static async Task DemonstrateAsync()
    {
        var context = new DefaultHttpContext();
        context.Request.Headers["X-Api-Key"] = "secret-key";
        context.Request.Headers[HeaderNames.UserAgent] = "Cuemon Docs";

        var throttlingOptions = new ThrottlingSentinelOptions
        {
            ContextResolver = _ => "client-1",
            Quota = new ThrottleQuota(2, TimeSpan.FromMinutes(1))
        };

        await Decorator.Enclose(context).InvokeThrottlerSentinelAsync(new MemoryThrottlingCache(), throttlingOptions);

        var apiKeyOptions = new ApiKeySentinelOptions
        {
            AllowedKeys = new List<string> { "secret-key" }
        };

        await Decorator.Enclose(context).InvokeApiKeySentinelAsync(apiKeyOptions);

        var userAgentOptions = new UserAgentSentinelOptions();
        await Decorator.Enclose(context).InvokeUserAgentSentinelAsync(userAgentOptions);

        var handler = new HttpExceptionDescriptorResponseHandler(
            new HttpMediaTypeHeaderValue("text/plain"),
            exceptionDescriptor => new HttpResponseMessage((HttpStatusCode)exceptionDescriptor.StatusCode)
            {
                Content = new StringContent(exceptionDescriptor.Message)
            });

        var descriptor = new HttpExceptionDescriptor(new InvalidOperationException("Bad request"), StatusCodes.Status400BadRequest);
        await Decorator.Enclose(context).WriteExceptionDescriptorResponseAsync(handler, descriptor, CancellationToken.None);

        Console.WriteLine(context.Response.StatusCode);
    }
}

Methods

InvokeApiKeySentinelAsync(IDecorator<HttpContext>, ApiKeySentinelOptions)

Common API key logic for ASP.NET Core and ASP.NET Core MVC. Not intended to be used directly from your code.

public static Task InvokeApiKeySentinelAsync(this IDecorator<HttpContext> decorator, ApiKeySentinelOptions options)

Parameters

decorator IDecorator<HttpContext>

The IDecorator<T> to extend.

options ApiKeySentinelOptions

The configured options.

Returns

Task

InvokeThrottlerSentinelAsync(IDecorator<HttpContext>, IThrottlingCache, ThrottlingSentinelOptions)

Common throttler operation logic for ASP.NET Core and ASP.NET Core MVC. Not intended to be used directly from your code.

public static Task InvokeThrottlerSentinelAsync(this IDecorator<HttpContext> decorator, IThrottlingCache tc, ThrottlingSentinelOptions options)

Parameters

decorator IDecorator<HttpContext>

The IDecorator<T> to extend.

tc IThrottlingCache

The IThrottlingCache implementation.

options ThrottlingSentinelOptions

The configured options.

Returns

Task

InvokeUserAgentSentinelAsync(IDecorator<HttpContext>, UserAgentSentinelOptions)

Common user agent logic for ASP.NET Core and ASP.NET Core MVC. Not intended to be used directly from your code.

public static Task InvokeUserAgentSentinelAsync(this IDecorator<HttpContext> decorator, UserAgentSentinelOptions options)

Parameters

decorator IDecorator<HttpContext>

The IDecorator<T> to extend.

options UserAgentSentinelOptions

The configured options.

Returns

Task

WriteExceptionDescriptorResponseAsync(IDecorator<HttpContext>, HttpExceptionDescriptorResponseHandler, HttpExceptionDescriptor, CancellationToken)

Invokes and write the result from the specified handler to the response body. Not intended to be used directly from your code.

public static Task WriteExceptionDescriptorResponseAsync(this IDecorator<HttpContext> decorator, HttpExceptionDescriptorResponseHandler handler, HttpExceptionDescriptor exceptionDescriptor, CancellationToken ct)

Parameters

decorator IDecorator<HttpContext>

The IDecorator<T> to extend.

handler HttpExceptionDescriptorResponseHandler

The HttpExceptionDescriptorResponseHandler that holds the content negotiation response.

exceptionDescriptor HttpExceptionDescriptor

The HttpExceptionDescriptor that provides information about an Exception.

ct CancellationToken

The cancellation token that can be used by other objects or threads to receive notice of cancellation.

Returns

Task

A Task representing the asynchronous operation.

See Also