title: C#: Add Custom Properties In HttpContextAccessor
body: <p>Is there a way to add Custom properties in HttpContextAccessor in C# Net Core?</p>
  
  <p>Say, create new property called: Favoritefood Property, or CreatedByUser (coming from special function) and assign a value?
  this is simple example below,</p>
  
  <p>We want to eventually bring custom properties into the DbContext layer, not only audit related, etc.</p>
  
  <pre><code>public class AuditableDbContext: DbContext
  {
      private readonly IHttpContextAccessor _httpContextAccessor;
  
      public AuditableDbContext(DbContextOptions&lt;AuditableDbContext&gt; options, IHttpContextAccessor httpContextAccessor)
          : base(options)
      {
          _httpContextAccessor = httpContextAccessor;
      }
  
      private void ApplyCreatedBy()
      {
          var modifiedEntities = ChangeTracker.Entries&lt;IFood&gt;().Where(e =&gt; e.State == EntityState.Added);
          foreach (var entity in modifiedEntities)
          {
              entity.Property("Food").CurrentValue = GetFoodMethod();
          }
      }
  
      public override int SaveChanges()
      {
          ApplyCreatedBy();
          return base.SaveChanges();
      }
  </code></pre>
  
  <p><strong>Resources:</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/52854319/log-username-and-ip-with-custom-properties-in-application-insight-net-core-2-1">Log UserName and IP with Custom Properties in Application Insight .Net Core 2.1</a></p>
  
  <p><a href="https://stackoverflow.com/questions/47565882/add-custom-properties-to-claimsprincipal-user-in-asp-net-core-2">Add Custom properties to ClaimsPrincipal user in asp.net core 2</a></p>