Skip to main content

UnauthorizedResponse

Namespace: RA.Utilities.Api.Response

The UnauthorizedResponse class creates a standardized 401 Unauthorized response. It derives from Response<T> with T = ErrorResult and is used when authentication is missing or invalid.

🎯 Purpose​

Use UnauthorizedResponse when the request lacks valid credentials — for example, when a token is missing, expired, or rejected.

  • Standard envelope: ResponseCode = 401, ResponseType = ResponseType.Unauthorized, ResponseMessage = "Authentication failed or is missing."
  • Optional payload: Result can be null or an ErrorResult describing the authentication failure.

Constructors​

ConstructorResultResponseCodeResponseMessage
UnauthorizedResponse(int responseCode, string responseMessage)nullresponseCoderesponseMessage
UnauthorizedResponse(ErrorResult? result = null)result401"Authentication failed or is missing."
UnauthorizedResponse(ErrorResult result, int? responseCode, string responseMessage = "")resultresponseCode ?? 401responseMessage ?? standard (the "" default is used as-is)

ResponseType is always ResponseType.Unauthorized (JSON 10). The optional result parameter means new UnauthorizedResponse() is valid.

Usage​

using Microsoft.AspNetCore.Mvc;
using RA.Utilities.Api.Response;

[HttpGet]
public IActionResult GetProfile()
{
if (User.Identity?.IsAuthenticated != true)
{
return Unauthorized(new UnauthorizedResponse(new ErrorResult
{
ErrorCode = "Unauthorized",
ErrorMessage = "User is not authenticated."
}));
}

return Ok(new SuccessResponse<UserProfile>(_service.GetProfile(User)));
}

Example JSON Output​

{
"responseCode": 401,
"responseType": 10,
"responseMessage": "Authentication failed or is missing.",
"result": {
"errorCode": "Unauthorized",
"errorMessage": "User is not authenticated."
}
}

With JsonStringEnumConverter, responseType is "Unauthorized".