0

I have some log that currently implemented, and my task is to implement to obscure the sensitive data(firstname, lastname, birthday, gender, etc…). I already finish the obscuring process, but currently I am having problem when a common model object is used by multiple class.

My code in obscuring:

public class SensitiveField : JsonConverter
{
    private bool _hideData;
    public SensitiveField()
    {
        _hideData = true;
    }

    public SensitiveField(bool hideData)
    {
        _hideData = hideData;
    }

    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(_hideData ? "*****" : value.ToString());
    }
}

Sample scenario:

public class CommonClass
{
    [JsonConverter(typeof(SensitiveField))]
    public string col1 { get; set; }

    [JsonConverter(typeof(SensitiveField))]
    public string col2 { get; set; }
}

public class TestModel
{
    public string Username { get; set; }

    [JsonConverter(typeof(SensitiveField))]
    public string Password { get; set; }

    [JsonConverter(typeof(SensitiveField))]
    public int Age { get; set; }

    public CommonClass SensitiveData { get; set; }
    public CommonClass OpenData { get; set; }
}

As you notice, the CommonClass is implemented in the SensitiveField which will mask the data. In TestModel we use the CommonClass twice, one is obscured while one will not, if I use the JsonConvert.SerializeObject(testModel) both will be obscure.

How can I ignore the obscuring in the open field?

Florian
  • 1,019
  • 6
  • 22
vic
  • 1
  • 2

0 Answers0