60 lines
2.1 KiB
C#
60 lines
2.1 KiB
C#
using Koogle.Domain.Interfaces;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Koogle.Infrastrcuture.Services;
|
|
|
|
public class CurrentUserService : ICurrentUserService
|
|
{
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine neue Instanz des CurrentUserService.
|
|
/// </summary>
|
|
/// <param name="httpContextAccessor">Der HTTP-Kontext-Accessor.</param>
|
|
public CurrentUserService(IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public int UserId
|
|
{
|
|
get
|
|
{
|
|
//var userIdClaim = _httpContextAccessor.HttpContext?.UserProfile?.FindFirst(ClaimTypes.NameIdentifier)?.Value
|
|
// ?? _httpContextAccessor.HttpContext?.UserProfile?.FindFirst("sub")?.Value
|
|
// ?? _httpContextAccessor.HttpContext?.UserProfile?.FindFirst("oid")?.Value;
|
|
var userIdClaim = _httpContextAccessor.HttpContext?.User?.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
|
|
|
|
return 0;
|
|
//if (string.IsNullOrEmpty(userIdClaim))
|
|
// return Guid.Empty;
|
|
|
|
//// Versuche zuerst als Guid zu parsen (für interne UserProfile-IDs)
|
|
//if (Guid.TryParse(userIdClaim, out var userId))
|
|
// return userId;
|
|
|
|
//// Falls es eine Azure AD Object ID ist, generiere eine deterministische Guid daraus
|
|
//return GenerateDeterministicGuid(userIdClaim);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public bool IsAuthenticated =>
|
|
_httpContextAccessor.HttpContext?.User?.Identity?.IsAuthenticated ?? false;
|
|
|
|
/// <summary>
|
|
/// Generiert eine deterministische Guid aus einem String.
|
|
/// </summary>
|
|
private static Guid GenerateDeterministicGuid(string input)
|
|
{
|
|
using var md5 = System.Security.Cryptography.MD5.Create();
|
|
var hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));
|
|
return new Guid(hash);
|
|
}
|
|
} |