HttpContextExtension.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Security.Claims;
  2. namespace Library.Extensions;
  3. public static class HttpContextExtension
  4. {
  5. // 회원 ID 조회
  6. public static int GetMemberID(this HttpContext context)
  7. {
  8. return int.TryParse(context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value, out var id) ? id : 0;
  9. }
  10. // 회원 이름 조회
  11. public static string? UserName(this HttpContext context) => context.User.Identity?.Name;
  12. // 이전 요청 URL 조회
  13. public static string? GetReferer(this HttpContext context)
  14. {
  15. if (context.Request.Headers.TryGetValue("Referer", out var referer) && !string.IsNullOrWhiteSpace(referer))
  16. {
  17. return referer.ToString();
  18. }
  19. return null;
  20. }
  21. // Client IP 조회 — Cloudflare → IIS → backend reverse proxy 환경 대응
  22. // 우선순위:
  23. // 0) X-Original-Client-IP (Next.js proxy 가 Cloudflare 더블 경유 우회용 커스텀 헤더 ⇒ 진짜 user IP)
  24. // 1) CF-Connecting-IP (Cloudflare 가 보장하는 client IP, 단 더블 경유 시 Next.js server IP)
  25. // 2) X-Real-IP (일부 proxy 가 채움)
  26. // 3) X-Forwarded-For (comma-separated 의 첫 값만)
  27. // 4) RemoteIpAddress (ForwardedHeaders middleware 가 채워주는 경우)
  28. // IPv4-mapped IPv6 (`::ffff:127.0.0.1`) → `127.0.0.1` 로 정규화.
  29. public static string? GetClientIP(this HttpContext context)
  30. {
  31. // 0순위: Next.js Route Handler 가 명시 forward 한 user IP
  32. // (developers/app/api/.../route.ts 가 cf-connecting-ip 추출 후 이 헤더로 backend 전달)
  33. if (context.Request.Headers.TryGetValue("X-Original-Client-IP", out var orig) && !string.IsNullOrWhiteSpace(orig))
  34. {
  35. return NormalizeIP(orig.ToString().Trim());
  36. }
  37. if (context.Request.Headers.TryGetValue("CF-Connecting-IP", out var cf) && !string.IsNullOrWhiteSpace(cf))
  38. {
  39. return NormalizeIP(cf.ToString().Trim());
  40. }
  41. if (context.Request.Headers.TryGetValue("X-Real-IP", out var xri) && !string.IsNullOrWhiteSpace(xri))
  42. {
  43. return NormalizeIP(xri.ToString().Trim());
  44. }
  45. var forwardedFor = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
  46. if (!string.IsNullOrWhiteSpace(forwardedFor))
  47. {
  48. // comma-separated 의 가장 왼쪽 = 원래 client (RFC 7239 / standard)
  49. var first = forwardedFor.Split(',', 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
  50. if (!string.IsNullOrWhiteSpace(first))
  51. {
  52. return NormalizeIP(first);
  53. }
  54. }
  55. return NormalizeIP(context.Connection.RemoteIpAddress?.ToString());
  56. }
  57. private static string? NormalizeIP(string? ip)
  58. {
  59. if (string.IsNullOrWhiteSpace(ip))
  60. {
  61. return null;
  62. }
  63. // IPv4-mapped IPv6 (e.g. "::ffff:127.0.0.1") → IPv4 만 추출
  64. if (System.Net.IPAddress.TryParse(ip, out var parsed))
  65. {
  66. if (parsed.IsIPv4MappedToIPv6)
  67. {
  68. return parsed.MapToIPv4().ToString();
  69. }
  70. return parsed.ToString();
  71. }
  72. return ip;
  73. }
  74. // User-agent 조회 — Next.js Route Handler 가 forward 한 X-Original-User-Agent 최우선.
  75. public static string? GetUserAgent(this HttpContext context)
  76. {
  77. if (context.Request.Headers.TryGetValue("X-Original-User-Agent", out var origUa) && !string.IsNullOrWhiteSpace(origUa))
  78. {
  79. return origUa.ToString();
  80. }
  81. if (context.Request.Headers.TryGetValue("User-Agent", out var userAgent) && !string.IsNullOrWhiteSpace(userAgent))
  82. {
  83. return userAgent.ToString();
  84. }
  85. return null;
  86. }
  87. // 상관관계 ID 조회
  88. public static string GetCorrelationID(this HttpContext context) => context.TraceIdentifier;
  89. }