Introduction
In ASP.NET Core Web API, the HttpContextAccessor class is a component that provides access to the current HTTP request and response context. It allows you to access various aspects of the HTTP request and response, such as headers, cookies, query parameters, and user claims.
Step 1. Add Dependency to your Program.cs

Code Example
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Step 2. Use of HttpContextAccessor
In the Give below example, we are getting the User, and After that, we are getting the company Id and User ID From the Auth Microservice.
Code Example
using AC_Projects_API_Domain_Layer.Models;
using AC_Projects_API_Repository_Layer.IRepository;
using AC_Projects_API_Service_Layer.IService;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace AC_Projects_API_Service_Layer.Services
{
public class ColumsService : ICustomService<Columns>
{
private readonly IRepository<Columns> _statusRepository;
public long LoggedInUserCompanyId;
public long LoggedInUserId;
private readonly IHttpContextAccessor _httpContextAccessor;
public ColumsService(
IRepository<Columns> statusRepository,
IMapper mapper,
IHttpContextAccessor httpContextAccessor
)
{
_statusRepository = statusRepository;
_httpContextAccessor = httpContextAccessor;
ClaimsIdentity user = (ClaimsIdentity)_httpContextAccessor.HttpContext.User.Identity;
LoggedInUserCompanyId = Convert.ToUInt32(user.FindFirst("CompanyId").Value.ToString());
LoggedInUserId = Convert.ToUInt32(user.FindFirst("UserId").Value.ToString());
}
public void Delete(Columns entity)
{
try
{
if (entity != null)
{
entity.ModifiedBy = LoggedInUserId;
entity.ModifiedDate = DateTime.Now;
_statusRepository.Delete(entity);
_statusRepository.SaveChanges();
}
}
catch (Exception)
{
throw;
}
}
public Columns Get(long Id)
{
try
{
var obj = _statusRepository.Get(Id);
if (obj != null)
{
return obj;
}
else
{
return null;
}
}
catch (Exception)
{
throw;
}
}
public IEnumerable<Columns> GetAll()
{
try
{
var obj = _statusRepository.GetAll();
if (obj != null)
{
return obj;
}
else
{
return null;
}
}
catch (Exception)
{
throw;
}
}
public Columns Insert(Columns entity)
{
try
{
if (entity != null)
{
entity.CreatedBy = LoggedInUserId;
entity.CreatedDate = DateTime.Now;
entity.CompanyId = LoggedInUserCompanyId;
_statusRepository.Insert(entity);
_statusRepository.SaveChanges();
return entity;
}
return null;
}
catch (Exception)
{
throw;
}
}
public void Remove(Columns entity)
{
try
{
if (entity != null)
{
_statusRepository.Remove(entity);
_statusRepository.SaveChanges();
}
}
catch (Exception)
{
throw;
}
}
public void Update(Columns entity)
{
try
{
if (entity != null)
{
entity.ModifiedBy = LoggedInUserId;
entity.ModifiedDate = DateTime.Now;
_statusRepository.Update(entity);
_statusRepository.SaveChanges();
}
}
catch (Exception)
{
throw;
}
}
}
}
Join the conversation! Your thoughts help the community grow.