feat(ExceptionHandlingMiddleware): Add to handle exceptions by middleware

This commit is contained in:
2025-07-30 17:16:10 +02:00
parent 78f2788388
commit 63df235943
8 changed files with 175 additions and 112 deletions

View File

@@ -1,76 +1,74 @@
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Security.Claims;
using WorkFlow.API.Attributes;
namespace WorkFlow.API.Controllers
namespace WorkFlow.API.Controllers;
[APIKeyAuth]
public static class ControllerExtensions
{
[APIKeyAuth]
public static class ControllerExtensions
public static bool TryGetUserId(this ClaimsPrincipal user, out int id) => int.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out id);
public static bool TryGetUsername(this ClaimsPrincipal user, out string username)
{
public static bool TryGetUserId(this ClaimsPrincipal user, out int id) => int.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out id);
var value = user.FindFirstValue(ClaimTypes.Name);
public static bool TryGetUsername(this ClaimsPrincipal user, out string username)
if (value is null)
{
var value = user.FindFirstValue(ClaimTypes.Name);
if (value is null)
{
username = string.Empty;
return false;
}
else
{
username = value;
return true;
}
username = string.Empty;
return false;
}
public static bool TryGetName(this ClaimsPrincipal user, out string name)
else
{
var value = user.FindFirstValue(ClaimTypes.Surname);
if (value is null)
{
name = string.Empty;
return false;
}
else
{
name = value;
return true;
}
username = value;
return true;
}
}
public static bool TryGetPrename(this ClaimsPrincipal user, out string prename)
public static bool TryGetName(this ClaimsPrincipal user, out string name)
{
var value = user.FindFirstValue(ClaimTypes.Surname);
if (value is null)
{
var value = user.FindFirstValue(ClaimTypes.GivenName);
if (value is null)
{
prename = string.Empty;
return false;
}
else
{
prename = value;
return true;
}
name = string.Empty;
return false;
}
public static bool TryGetEmail(this ClaimsPrincipal user, out string email)
else
{
var value = user.FindFirstValue(ClaimTypes.Email);
name = value;
return true;
}
}
if (value is null)
{
email = string.Empty;
return false;
}
else
{
email = value;
return true;
}
public static bool TryGetPrename(this ClaimsPrincipal user, out string prename)
{
var value = user.FindFirstValue(ClaimTypes.GivenName);
if (value is null)
{
prename = string.Empty;
return false;
}
else
{
prename = value;
return true;
}
}
public static bool TryGetEmail(this ClaimsPrincipal user, out string email)
{
var value = user.FindFirstValue(ClaimTypes.Email);
if (value is null)
{
email = string.Empty;
return false;
}
else
{
email = value;
return true;
}
}
}