JSON Data Masking is a library for .NET Core applications that simplifies the masking process of PII/sensitive information.
$ dotnet add package JsonDataMaskingJSON Data Masking is a library for .NET Core applications that simplifies the masking process of PII/sensitive information.
After installing the Nuget package in your project, you need to take the following steps:
Add the [SensitiveData] attribute in your class properties to indicate what should be masked, and use its available fields to configure the masking:
true to keep the property length when masking its value. By default this setting is set to false.N characters of the property, not masking them. The default value is 0.N characters of the property, not masking them. The default value is 0.* is used.PS.: These customization fields only work for fields with a
stringbase type.
Call the JsonMask.MaskSensitiveData() function, passing in your object instance as a parameter.
This library supports masking of the following base types:
string fields, which are masked following the rules detailed in the Usage section.bool, (s)byte, (u)short, (u)int, (u)long, float, double, decimal, char, Datetime, DatetimeOffset, and Guid, which are set to their respective default values when having the [SensitiveData] attribute.The library also supports the masking of some collections, such as:
List<T>, where T is a class or string.IEnumerable<T>, where T is a class or string.Dictionary<string, string>.Array, ArrayList<T>, etc., is NOT supported.Nested class properties are also masked, independently of depth.
public class PropertiesExamples
{
/// 123456789 results in "*****"
[SensitiveData]
public string DefaultMask { get; set; }
/// 123456789 results in "REDACTED"
[SensitiveData(SubstituteText = "REDACTED")]
public string SubstituteMask { get; set; }
/// 123456789 results in "123*****789"
[SensitiveData(ShowFirst = 3, ShowLast = 3)]
public string ShowCharactersMask { get; set; }
/// 123456789 results in "#########"
[SensitiveData(PreserveLength = true, Mask = "#")]
public string PreserveCustomMask { get; set; }
}
var maskedData = JsonMask.MaskSensitiveData(data);