I have a solution with two projects, my API layer and my Data layer.
The Data layer is using Entity Framework, and does not know about ASP.NET WebApi.
The API layer is using ASP.NET WebApi.
I am planning on using Audit.NET to audit record changes.
Currently, I have installed Audit.NET, Audit.EntityFramework and Audit.WebApi in my API project.
I wish to use the Audit Filter Attribute. (config.Filters.Add(new AuditApiAttribute());
)
My issue is, when I come to populate the EntityLog object, I also would like to populate the UserId field with the Id of the user currently performing an authorized action, or null if the action is anonymous.
As part of my Startup.cs, I run this function to configure Audit.NET:
private void ConfigureAudit()
{
Audit.Core.Configuration.Setup()
.UseEntityFramework(x => x
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.UserId = ???;
// other fields...
})
.IgnoreMatchedProperties()
);
}
Obviously at this point I cannot use HttpContext.Current.User.Identity.GetUserId()
as I'm not inside a controller, and I don't see a way of passing the UserId into the Audit event.
How can I retrieve the UserId at this point?