---
title: "How to get user MetaData from user request on API?"  
description: "How to get user MetaData from user request on API?"  
author: "Ravi Vishwakarma"  
published: 2024-12-16  
updated: 2024-12-16  
canonical: https://www.mindstick.com/forum/161059/how-to-get-user-metadata-from-user-request-on-api  
category: "web api"  
tags: ["api(s)", "web api", "rest api"]  
reading_time: 2 minutes  

---

# How to get user MetaData from user request on API?

## In C#/.NET Core

How to get [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) [MetaData](https://www.mindstick.com/articles/334241/the-power-of-metadata-how-it-shapes-google-search-results) from user requests on [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph), Just like [IP Address](https://www.mindstick.com/articles/188405/difference-between-mac-address-and-ip-address), [Location](https://www.mindstick.com/blog/11636/relocating-business-or-office-to-another-location), Lattitute Laongitute, Regin, [Country](https://yourviews.mindstick.com/view/85543/do-you-think-that-french-will-be-islamic-country), State, [Url](https://www.mindstick.com/forum/436/url-redirection) Reffer, etc

## Replies

### Reply by Anubhav Sharma

In .NET Core, you can extract user metadata from the `HttpRequest` object in an API controller. Here’s how you can access various types of metadata:

## 1. IP Address

To get the user's IP address:

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

## 2. User Agent

To get the user agent string from the request headers:

```cs
var userAgent = HttpContext.Request.Headers["User-Agent"].ToString();
```

## 3. Request Headers

To access all headers:

```cs
foreach (var header in HttpContext.Request.Headers)
{
    Console.WriteLine($"{header.Key}: {header.Value}");
}
```

## 4. Referrer URL

To get the URL of the page that referred the user:

```cs
var referrer = HttpContext.Request.Headers["Referer"].ToString();
```

## 5. Authorization Information

To get the `Authorization` header (e.g., JWT or Bearer tokens):

```cs
var authorizationHeader = HttpContext.Request.Headers["Authorization"].ToString();
```

## 6. Query Parameters

To get query parameters:

```cs
var queryParams = HttpContext.Request.Query;
foreach (var param in queryParams)
{
    Console.WriteLine($"{param.Key}: {param.Value}");
}
```

## 7. Custom Metadata in Headers

If your client sends custom metadata in the headers (e.g., `X-Custom-Metadata`), you can access it like this:

```cs
var customMetadata = HttpContext.Request.Headers["X-Custom-Metadata"].ToString();
```

#### Example Controller

Here’s an example controller that gathers and returns metadata:

```cs
[ApiController]
[Route("api/[controller]")]
public class MetadataController : ControllerBase
{
    [HttpGet]
    public IActionResult GetMetadata()
    {
        var metadata = new
        {
            IpAddress = HttpContext.Connection.RemoteIpAddress?.ToString(),
            UserAgent = HttpContext.Request.Headers["User-Agent"].ToString(),
            Referrer = HttpContext.Request.Headers["Referer"].ToString(),
            Authorization = HttpContext.Request.Headers["Authorization"].ToString(),
            QueryParams = HttpContext.Request.Query.ToDictionary(q => q.Key, q => q.Value.ToString())
        };

        return Ok(metadata);
    }
}
```

#### Notes

- If the API is behind a proxy/load balancer, you might need to use forwarded headers to get the correct client IP. Enable and configure the `ForwardedHeaders` middleware for this:

```cs
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
```

- Ensure your middleware configuration matches your hosting setup (e.g., NGINX, IIS).

Thanks for reading.


---

Original Source: https://www.mindstick.com/forum/161059/how-to-get-user-metadata-from-user-request-on-api

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
