[英]AuditIgnore on IdentityUser - Audit.Net
我正在使用Audit.Net来跟踪更改。 很好的图书馆
通过使用Audit.EntityFramework扩展,我们可以忽略要跟踪的属性。
public class User
{
public int Id { get; set; }
[AuditIgnore]
public string Password { get; set; }
...
}
但我有问题
Audit.EntityFramework
扩展。 只需使用我自己的自定义数据提供程序获取 json 格式。 所以[AuditIgnore]
不能在这里工作。IdentityUser
类上的password property
?这是我的自定义数据提供者
public class MyCustomDataProvider : AuditDataProvider
{
public override object InsertEvent(AuditEvent auditEvent)
{
var AuditEventInJson = auditEvent.ToJson();
//Save into to DB;
}
}
ToJson()
方法将在内部使用JSON.NET库来序列化事件,并将使用来自静态属性Audit.Core.Configuration.JsonSettings
的默认设置
因此,如果您可以更改包含密码的类的代码,则可以使用JsonIgnore
属性:
public class User
{
[JsonIgnore]
public string Password { get; set; }
...
}
或者,如果您无法装饰该属性,您可以创建一个自定义的Contract Resolver来一般地忽略属性,例如忽略任何名为“Password”的属性:
class MyCustomResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var props = base.CreateProperties(type, memberSerialization);
return props.Where(p => p.PropertyName != "Password").ToList();
}
}
为了使其与 Audit.NET ToJson()
方法一起使用,您需要在 Audit.NET 全局配置上设置合同解析器,如下所示:
Audit.Core.Configuration.JsonSettings.ContractResolver = new MyCustomResolver();
第三个选项是附加一个自定义操作以在事件保存发生之前删除要忽略的属性,例如:
Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaving, scope =>
{
var efEvent = scope.Event.GetEntityFrameworkEvent();
efEvent.Entries.ForEach(e =>
{
e.Changes.RemoveAll(ch => ch.ColumnName == "Password");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.