Core Web API
Core Web API
net/publication/371071377
CITATIONS READS
0 874
1 author:
SEE PROFILE
All content following this page was uploaded by Sardar Mudassar Ali Khan on 26 May 2023.
Contents
HttpContextAccessor in Asp.NET Core Web API..................................................................................... 1
Step 1: First of all you need to add Dependency to your Program.cs ................................................ 2
Code Example ..................................................................................................................................... 2
Step 2 Use of HttpContextAccessor .................................................................................................... 2
Code Example ..................................................................................................................................... 2
Why’d we Use HttpContextAccessor .................................................................................................. 5
Accessing HTTP Request and Response: ......................................................................................... 5
Authentication and Authorization: ................................................................................................. 5
Logging and Diagnostics: ................................................................................................................. 5
Custom Middleware and Filters: ..................................................................................................... 5
Integration with External Services: ................................................................................................. 5
Conclusion ........................................................................................................................................... 5
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.
Code Example
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
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;
}
}
Although the "HttpContextAccessor" offers practical access to the HTTP context, it should only be
used sparingly. By exposing the HTTP context too broadly in your application, you run the risk of
creating security holes or adding dependencies that make it harder to maintain your code. The use
of "HttpContextAccessor" in your application should, therefore, be carefully considered in terms of
its scope and objectives.
Conclusion
The ASP.NET Core Web API's `ttpContextAccessor` gives users access to the current HTTP request
and response context. It enables you to carry out actions based on authentication and authorization,
alter the response, and retrieve information about the request. It is frequently employed for logging,
diagnostics, developing unique middleware, interfacing with outside services, and other purposes. It
should be utilized cautiously, considering security issues, and keeping a clean codebase.