-
Notifications
You must be signed in to change notification settings - Fork 0
In logging set ClientIp from request header X-Forwarded-For #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -107,9 +107,11 @@ | |||||
| var clientId = httpContext.User?.Claims.FirstOrDefault(claim => claim.Type == "clientId")?.Value; | ||||||
| var organizationId = httpContext.User?.Claims.FirstOrDefault(claim => claim.Type == "organizationid")?.Value; | ||||||
| var queryString = httpContext.Request.QueryString.HasValue ? httpContext.Request.QueryString.Value : ""; | ||||||
| var xForwardedFor = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault(); | ||||||
|
|
||||||
| context.Set("CorrelationId", correlationId); | ||||||
| context.Set("ClientId", clientId); | ||||||
| context.Set("ClientIp", !string.IsNullOrEmpty(xForwardedFor) ? xForwardedFor : httpContext.Connection.RemoteIpAddress?.ToString()); | ||||||
|
||||||
| context.Set("ClientIp", !string.IsNullOrEmpty(xForwardedFor) ? xForwardedFor : httpContext.Connection.RemoteIpAddress?.ToString()); | |
| context.Set("ClientIp", httpContext.Connection.RemoteIpAddress?.ToString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The X-Forwarded-For header can contain multiple IP addresses separated by commas (client, proxy1, proxy2, etc.). Using the entire header value could log internal proxy IPs or allow header spoofing. Consider extracting only the first IP address using
xForwardedFor.Split(',')[0].Trim()to get the original client IP.