Home > Backend Development > C++ > How to Get the Client IP Address in ASP.NET Core?

How to Get the Client IP Address in ASP.NET Core?

Patricia Arquette
Release: 2025-01-17 08:01:14
Original
656 people have browsed it

How to Get the Client IP Address in ASP.NET Core?

Retrieving the Client IP Address in ASP.NET Core

Original Problem:

The traditional method, Request.ServerVariables["REMOTE_ADDR"], for obtaining the client IP address in ASP.NET Core MVC 6 is now obsolete.

Updated Solution:

Due to recent API updates in ASP.NET Core, a new approach is necessary to retrieve the client's IP address. As noted by Damien Edwards, the following code provides the correct solution:

<code class="language-csharp">var remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;</code>
Copy after login

This revised method utilizes the Connection property within the HTTP context to successfully retrieve the remote IP address, effectively replacing the outdated Request.ServerVariables["REMOTE_ADDR"] method.

The above is the detailed content of How to Get the Client IP Address in ASP.NET Core?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template