Add "OrDefault" methods for user claim retrieval

Introduce new extension methods in `ControllerExtensions` to safely extract user information from a `ClaimsPrincipal`. Updated methods for ID, username, surname, given name, and email to return `null` if not found, enhancing flexibility. Updated `EnvelopeReceiverController` to utilize these new methods for improved handling of absent user claims.
This commit is contained in:
Developer 02
2025-05-08 10:56:46 +02:00
parent 2db0748e60
commit 3a1fe45524
2 changed files with 43 additions and 9 deletions

View File

@@ -86,12 +86,12 @@ public class EnvelopeReceiverController : ControllerBase
{
try
{
var username = User.GetUsername();
var username = User.GetUsernameOrDefault();
if (username is null)
{
_logger.LogError(@"Envelope Receiver dto cannot be sent because username claim is null. Potential authentication and authorization error. The value of other claims are [id: {id}], [username: {username}], [name: {name}], [prename: {prename}], [email: {email}].",
User.GetId(), User.GetUsername(), User.GetName(), User.GetPrename(), User.GetEmail());
User.GetId(), User.GetUsernameOrDefault(), User.GetNameOrDefault(), User.GetPrenameOrDefault(), User.GetEmailOrDefault());
return StatusCode(StatusCodes.Status500InternalServerError);
}