Context
I am trying wireup Audit.Net in an MVC5 app with .Net Framework 4.8.
Dataprovider: EntityFramework
Output: Cosmos
DI Provider: Autofac
The Problem
I have tried the following call backs to try and write the username to the AuditEvent.
Configuration.AddOnCreatedAction(scope =>
{
var username = HttpContext.Current.User.Identity.GetUserId();
scope.SetCustomField("User2", username);
});
Configuration.AddOnSavingAction(scope =>
{
var username = HttpContext.Current.User.Identity.GetUserId();
scope.SetCustomField("User2", username);
});
Configuration.AddOnSavingAction(scope =>
{
var user = AutofacDependencyResolver.Current.Resolve<IPrincipal>();
scope.SetCustomField("User2", user.Identity.GetUserId());
});
These for work for synchronous calls to dbContext.SaveChanges(), when SaveChangesAsync() is called then I cannot access the current HttpContext as it is always null.
Any suggestions?