Home >Backend Development >C++ >How Do I Get the Client's IP Address in ASP.NET Core?

How Do I Get the Client's IP Address in ASP.NET Core?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-17 08:09:09291browse

How Do I Get the Client's IP Address in ASP.NET Core?

Retrieving Client IP Addresses in ASP.NET Core Applications

In older ASP.NET versions, accessing the client's IP address was simple using Request.ServerVariables["REMOTE_ADDR"]. This method, however, is obsolete in ASP.NET Core.

The updated approach leverages the API's new structure. As noted by Damien Edwards, the client's IP address can be obtained using this code snippet:

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

This revised method offers a robust and dependable way to retrieve the client IP address within the context of ASP.NET Core applications.

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

Statement:
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