| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System.Security.Claims;
- namespace Library.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) => 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) => context.TraceIdentifier;
- }
- }
|