Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string GetIpAddress(this HttpContext ctx)
- {
- try
- {
- if (!string.IsNullOrEmpty(ctx.Request.ServerVariables["HTTP_CLIENT_IP"]))
- {
- return ctx.Request.ServerVariables["HTTP_CLIENT_IP"];
- }
- if (!string.IsNullOrEmpty(ctx.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
- {
- var addresses = ctx.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new[] { ',' },
- StringSplitOptions.RemoveEmptyEntries).ToList();
- return addresses.First();
- }
- return ctx.Request.ServerVariables["REMOTE_ADDR"];
- }
- catch (Exception)
- {
- // Log exception
- throw;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement