refactor(api): Aktualisierung von ControllerExtensions zur Nutzung von ClaimsPrincipal anstelle von ControllerBase
- Refactoring der Erweiterungsmethoden, sodass sie auf `ClaimsPrincipal` anstatt auf `ControllerBase` basieren. - Alle Verweise und Verwendungen dieser Erweiterungsmethoden im gesamten Code aktualisiert. - Dies verbessert die Flexibilität und entkoppelt das Abrufen von Benutzeransprüchen von den Controllern.
This commit is contained in:
@@ -5,20 +5,20 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
|
|||||||
{
|
{
|
||||||
public static class ControllerExtensions
|
public static class ControllerExtensions
|
||||||
{
|
{
|
||||||
public static int? GetId(this ControllerBase controller)
|
public static int? GetId(this ClaimsPrincipal user)
|
||||||
=> int.TryParse(controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value, out int result)
|
=> int.TryParse(user.FindFirst(ClaimTypes.NameIdentifier)?.Value, out int result)
|
||||||
? result : null;
|
? result : null;
|
||||||
|
|
||||||
public static string? GetUsername(this ControllerBase controller)
|
public static string? GetUsername(this ClaimsPrincipal user)
|
||||||
=> controller.User.FindFirst(ClaimTypes.Name)?.Value;
|
=> user.FindFirst(ClaimTypes.Name)?.Value;
|
||||||
|
|
||||||
public static string? GetName(this ControllerBase controller)
|
public static string? GetName(this ClaimsPrincipal user)
|
||||||
=> controller.User.FindFirst(ClaimTypes.Surname)?.Value;
|
=> user.FindFirst(ClaimTypes.Surname)?.Value;
|
||||||
|
|
||||||
public static string? GetPrename(this ControllerBase controller)
|
public static string? GetPrename(this ClaimsPrincipal user)
|
||||||
=> controller.User.FindFirst(ClaimTypes.GivenName)?.Value;
|
=> user.FindFirst(ClaimTypes.GivenName)?.Value;
|
||||||
|
|
||||||
public static string? GetEmail(this ControllerBase controller)
|
public static string? GetEmail(this ClaimsPrincipal user)
|
||||||
=> controller.User.FindFirst(ClaimTypes.Email)?.Value;
|
=> user.FindFirst(ClaimTypes.Email)?.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,7 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var id = this.GetId();
|
var id = User.GetId();
|
||||||
|
|
||||||
if (id is int intId)
|
if (id is int intId)
|
||||||
return await _envelopeService.ReadByIdAsync(intId).ThenAsync(
|
return await _envelopeService.ReadByIdAsync(intId).ThenAsync(
|
||||||
|
|||||||
@@ -24,12 +24,12 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var username = this.GetUsername();
|
var username = User.GetUsername();
|
||||||
|
|
||||||
if (username is null)
|
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}].",
|
_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}].",
|
||||||
this.GetId(), this.GetUsername(), this.GetName(), this.GetPrename(), this.GetEmail());
|
User.GetId(), User.GetUsername(), User.GetName(), User.GetPrename(), User.GetEmail());
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user