I've structured my project using DDD, it looks like this:
| CompanyName.API
| MyControllerThatRequiresJwtToken << Entry point
| CompanyName.Application
| CompanyName.Data
| EfCoreContext << I would like to get the claims here
| CompanyName.Domain
| CompanyName.IoC
| CompanyName.Test
| CompanyName.UI
I am using Z.EntityFramework.Plus.Audit.EFCore to audit all data changes. I added it to the CompanyName.Data project as this is where my EF Context lives.
Problem is: all requests in the API require a JWT token. I'd like to set the username of the person sending the request in the Audit object that will be saved into the database, however I don't have access to the HttpContext in my data layer.
What would be the best approach to get this information? Injecting IHttpContextAccessor into the data layer perhaps? It doesn't sound like a good plan to make the data layer "Http dependent".
UPDATE
I am not sure how I'd pass it from the Controller to the context. I believe it would need to be injected somehow.
Snippet of EfCoreContext.cs
public override int SaveChanges()
{
var audit = new Audit();
audit.CreatedBy = "JWT Username"; // << I'd need it here
audit.PreSaveChanges(this);
var rowAffecteds = base.SaveChanges();
audit.PostSaveChanges();
if (audit.Configuration.AutoSavePreAction != null)
{
audit.Configuration.AutoSavePreAction(this, audit);
base.SaveChanges();
}
return rowAffecteds;
}