| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using Microsoft.AspNetCore.Http;
- using System.Security.Claims;
- namespace SharedKernel.Extensions
- {
- public static class HttpContextExtension
- {
- // 회원 ID 조회
- public static int GetMemberID(this HttpContext context)
- {
- return int.TryParse(context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value, out var id) ? id : 0;
- }
- // 회원 이름 조회
- public static string? UserName(this HttpContext context)
- {
- return context.User.Identity?.Name;
- }
- // 이전 요청 URL 조회
- public static string? GetReferer(this HttpContext context)
- {
- if (context.Request.Headers.TryGetValue("Referer", out var referer) && !string.IsNullOrWhiteSpace(referer))
- {
- return referer.ToString();
- }
- return null;
- }
- // Client IP 조회
- public static string? GetClientIP(this HttpContext context)
- {
- var forwardedFor = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
- if (!string.IsNullOrWhiteSpace(forwardedFor))
- {
- return forwardedFor;
- }
- return context.Connection.RemoteIpAddress?.ToString();
- }
- // User-agent 조회
- public static string? GetUserAgent(this HttpContext context)
- {
- if (context.Request.Headers.TryGetValue("User-Agent", out var userAgent) && !string.IsNullOrWhiteSpace(userAgent))
- {
- return userAgent.ToString();
- }
- return null;
- }
- // 상관관계 ID 조회
- public static string GetCorrelationID(this HttpContext context)
- {
- return context.TraceIdentifier;
- }
- }
- }
|