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

576 views 0 comments last modified about 8 months ago Raymond

dotnet core asp.net core

In this page

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"];

Related pages

ASP.NET Core 2 with Bootstrap 4 Bundler Minifier Issue: Expected semicolon or closing curly-brace found '-'

468 views   1 comments last modified about 5 months ago

Background When upgrading Bootstrap to v4.0.0 release, the bundler and minifier doesn’t work properly due to CSS variable is commonly used: :root{--blue:#007bff; About 26 errors will show up in the Error List with the following message: ...

View detail

Tuples in C# (4.x, 7.0, 7.1)

60 views   0 comments last modified about 3 months ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

View detail

Invoke Hadoop WebHDFS APIs in .NET Core

92 views   0 comments last modified about 3 months ago

Background Apache doesn't provide native official .NET APIs for Hadoop HDFS. The HTTP REST API supports the complete FileSystem / ...

View detail

Logging configuration in .NET core

183 views   0 comments last modified about 8 months ago

.NET core introduces a logging API that works with a number of logging frameworks. The built-in providers are configurable and extensible to support different level loggings.

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.