【发布时间】:2018-09-22 18:20:45
【问题描述】:
我有很多这样的控制器:
public class EntityController : Controller
{
private readonly IEntityRepository _entity;
public EntityController(IEntityRepository entity)
{
_entity = entity;
}
[Authorize]
[HttpPut("{id}")]
public async ValueTask<IActionResult> Put(int id, [FromBody] Entity entity)
{
if (entity == null || entity.Id != id) return BadRequest();
var updated = await _entity.Update(entity);
if (updated == null) return NotFound();
return Ok(updated);
}
}
我需要实现实体编辑(审计)历史记录。
而且,由于该方法被标记为[Authorize],我需要记录它是由哪个用户编辑的。
我正在查看Audit.NET,但我没有找到方法。
【问题讨论】:
-
一种方法是在 OnActionExecuting 或 OnActionExecuted 中使用操作过滤器。 gist.github.com/mgroves/1832983 之类的内容应该可以帮助您入门。
-
但是如何保存(旧值、新值、用户名)?它只是一个触发器
-
您从上下文中获取用户名,即
context.HttpContext.User.Identity.Name和来自context.ActionArguments的参数。假设您在模型中有 PK,您可以查找旧值。
标签: asp.net-core entity-framework-core audit-logging audit.net