I am trying to find out what the authorized user is currently trying to save the data and record their email.
I have a basic setup in my dbcontext that does the update for timestamps but i cannot figure out how to access the user information:
public override int SaveChanges()
{
AddTimestamps();
return base.SaveChanges();
}
public override async Task<int> SaveChangesAsync()
{
AddTimestamps();
return await base.SaveChangesAsync();
}
private void AddTimestamps()
{
var entities = ChangeTracker.Entries()
.Where(x => x.State == EntityState.Added || x.State == EntityState.Modified);
var currentUser = "dumm@dummy.com" // This i haven't been able to figure out how to retrieve
foreach (var entity in entities)
{
if (entity.State == EntityState.Added)
{
((BaseEntity)entity.Entity).DateCreated = DateTime.UtcNow;
((BaseEntity)entity.Entity).CreatedBy = currentUsername;
}
((BaseEntity)entity.Entity).DateModified = DateTime.UtcNow;
((BaseEntity)entity.Entity).ModifiedBy = currentUsername;
}
}
}
Bit more of a background this is all built on ASP Core 2 and EF Core. The DBContext is located in my APIContoso project, which utilises the custom Identity Provider called IDPContoso which is built on ASP Core 2 and Identity Server4.
How can I obtain the users email within the DBContext so i can record it?