I am trying to access logged in user id in Persistence layer. I am using Claim and want to capture centralize created by and created on, logic below is my code kindly suggest me best approach
public class DBContext : DbContext
{
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
var AddedEntities = ChangeTracker.Entries().Where(E => E.State == EntityState.Added).ToList();
AddedEntities.ForEach(E =>
{
var prop = E.Metadata.FindProperty("CreatedOn");
if (prop != null)
{
E.Property("CreatedOn").CurrentValue = DateTimeHelper.DateTimeNow;
// This is a Persistence layer and want here to get logged in user Id
// E.Property("CreatedBy").CurrentValue=loggedInUserIdfromClaim
}
});
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
}