Class ForbiddenException
- Namespace
- Cuemon.AspNetCore.Http
- Assembly
- Cuemon.AspNetCore.dll
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
ForbiddenException()
Initializes a new instance of the ForbiddenException class.
public ForbiddenException()
ForbiddenException(Exception)
Initializes a new instance of the ForbiddenException class.
public ForbiddenException(Exception innerException)
Parameters
innerExceptionExceptionThe exception that is the cause of the current exception.
ForbiddenException(string, Exception)
Initializes a new instance of the ForbiddenException class.
public ForbiddenException(string message, Exception innerException = null)
Parameters
messagestringThe message that describes the HTTP status code.
innerExceptionExceptionThe exception that is the cause of the current exception.