Да, вы можете заменить или замаскировать проверенные значения, чтобы скрыть конфиденциальную информацию.
Реализация MaskableAuditSerializer
:
public class MaskableAuditSerializer : IAuditSerializer, ITransientDependency
{
private readonly IAuditingConfiguration _configuration;
public MaskableJsonNetAuditSerializer(IAuditingConfiguration configuration)
{
_configuration = configuration;
}
public string Serialize(object obj)
{
var options = new JsonSerializerSettings
{
ContractResolver = new MaskableAuditingContractResolver(_configuration.IgnoredTypes)
};
return JsonConvert.SerializeObject(obj, options);
}
}
Реализация MaskableAuditingContractResolver
путем наследования AuditingContractResolver
:
public class MaskableAuditingContractResolver : AuditingContractResolver
{
public MaskableAuditingContractResolver(List<Type> ignoredTypes)
: base(ignoredTypes)
{
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (member.IsDefined(typeof(MaskedAuditedAttribute)))
{
property.ValueProvider = new MaskedValueProvider();
}
return property;
}
}
Реализация MaskedValueProvider
:
public class MaskedValueProvider : IValueProvider
{
public object GetValue(object target)
{
return "***";
}
public void SetValue(object target, object value)
{
throw new NotImplementedException();
}
}
Реализация MaskedAuditedAttribute
наследуя AuditedAttribute
:
public class MaskedAuditedAttribute : AuditedAttribute
{
}
Использование
public class LoginViewModel
{
[MaskedAudited]
public string Password { get; set; }
// ...
}