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!