Retrieve Http client request metadata like IP address and languages in asp.net core

visibility 8,912 access_time 2 years ago languageEnglish timeline Stats
timeline Stats
Page index 5.19
more_horiz

IP Address

In ASP.NET Core, Request.UserHostAddress has been removed though that attribute exists in the traditional ASP.NET applications.

We can use HttpContext.Connection to retrieve the remove client IP address:

var ipAddress = HttpContext.Connection.RemoteIpAddress?.ToString();

Note

This IP address may not be the direct IP address of the user.  For example, user may use VPN, proxy and etc.

User Agent

Through user agent, we can get users’ browser information.

var userAgent = Request.Headers["User-Agent"];

User Languages

To support localization and globalization, user language is also important. Accept-Language request header can be used for this purpose. However, this attribute is an array and even the element can include multiple languages separated by semi comma. For example, “en-US, en, *”.

var languages = Request.Headers["Accept-Language"];

info Last modified by Raymond 2 years ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

More from Kontext
Read Embedded Assembly Resource Files in .NET
visibility 1,685
thumb_up 0
access_time 11 months ago
Use NDepend to Improve your .NET Code Quality
visibility 1,006
thumb_up 0
access_time 2 years ago