44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Core_Archi.Helpers;
|
|
|
|
public class UserHelper
|
|
{
|
|
public string? ExtractBrowser(string? userAgent)
|
|
{
|
|
if (string.IsNullOrEmpty(userAgent)) return null;
|
|
|
|
if (userAgent.Contains("Chrome")) return "Chrome";
|
|
if (userAgent.Contains("Firefox")) return "Firefox";
|
|
if (userAgent.Contains("Safari") && !userAgent.Contains("Chrome")) return "Safari";
|
|
if (userAgent.Contains("Edge")) return "Edge";
|
|
if (userAgent.Contains("Opera")) return "Opera";
|
|
|
|
return "Other";
|
|
}
|
|
|
|
public string? ExtractOS(string? userAgent)
|
|
{
|
|
if (string.IsNullOrEmpty(userAgent)) return null;
|
|
|
|
if (userAgent.Contains("Windows")) return "Windows";
|
|
if (userAgent.Contains("Mac OS")) return "macOS";
|
|
if (userAgent.Contains("Linux")) return "Linux";
|
|
if (userAgent.Contains("Android")) return "Android";
|
|
if (userAgent.Contains("iOS") || userAgent.Contains("iPhone") || userAgent.Contains("iPad")) return "iOS";
|
|
|
|
return "Other";
|
|
}
|
|
|
|
public Guid GetUserIdFromClaims(HttpContext httpContext)
|
|
{
|
|
var userIdClaim = httpContext.User.FindFirst("userId")
|
|
?? httpContext.User.FindFirst("UserId")
|
|
?? httpContext.User.FindFirst("sub");
|
|
|
|
if (userIdClaim == null || !Guid.TryParse(userIdClaim.Value, out var userId))
|
|
throw new ArgumentException("User ID not found in token");
|
|
|
|
return userId;
|
|
}
|
|
} |