You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
717 B

  1. using Microsoft.Owin;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Web;
  5. namespace Common.Utility
  6. {
  7. public class GetClientIpAddress
  8. {
  9. public string GetIpAddress(HttpRequestMessage request)
  10. {
  11. if (request.Properties.ContainsKey("MS_HttpContext"))
  12. {
  13. return IPAddress.Parse(((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress).ToString();
  14. }
  15. if (request.Properties.ContainsKey("MS_OwinContext"))
  16. {
  17. return IPAddress.Parse(((OwinContext)request.Properties["MS_OwinContext"]).Request.RemoteIpAddress).ToString();
  18. }
  19. return null;
  20. }
  21. }
  22. }