Table of Contents

ForbiddenException Class

Definition

Namespace
Cuemon.AspNetCore.Http
Assemblies
Cuemon.AspNetCore.dll
Source
src/Cuemon.AspNetCore/Http/ForbiddenException.cs

The exception that is thrown when the client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401, the client's identity is known to the server.

public class ForbiddenException : HttpStatusCodeException, ISerializable
Inheritance
ForbiddenException
Implements
Inherited Members

Examples

The following example demonstrates how to use ForbiddenException to reject unauthorized access in an authorization filter.

using System;
using Cuemon.AspNetCore.Http;

namespace MyApp.Examples;

public class ForbiddenExceptionExample
{
    public void Demonstrate()
    {
        try
        {
            var userRole = "guest";
            if (userRole != "admin")
            {
                throw new ForbiddenException("Only administrators can perform this action.");
            }
        }
        catch (ForbiddenException ex)
        {
            Console.WriteLine(ex.StatusCode); // 403
            Console.WriteLine(ex.Message);    // Only administrators can perform this action.
        }
    }
}

Constructors

Name Description
ForbiddenException()

Initializes a new instance of the ForbiddenException class.

ForbiddenException(Exception)

Initializes a new instance of the ForbiddenException class.

ForbiddenException(string, Exception)

Initializes a new instance of the ForbiddenException class.

See Also