A library for data redaction in ControlR, providing tools to redact sensitive information from logs and outputs.
$ dotnet add package ControlR.Libraries.DataRedactionA lightweight library for redacting sensitive data in logs and outputs using Microsoft's data compliance framework.
This library provides tools to automatically redact sensitive information during logging operations, helping you maintain security and comply with data protection regulations. It uses Microsoft's Microsoft.Extensions.Compliance.Redaction package to seamlessly integrate with standard .NET logging infrastructure.
dotnet add package ControlR.Libraries.DataRedaction
Add the redactor to your service collection:
using ControlR.Libraries.DataRedaction;
var builder = WebApplication.CreateBuilder(args);
// Add redaction services
builder.Services.AddStarRedactor();
var app = builder.Build();
Use the ProtectedDataClassification attribute to mark properties that should be redacted:
public class User
{
public string Name { get; set; }
[ProtectedDataClassification]
public string Password { get; set; }
[ProtectedDataClassification]
public string ApiKey { get; set; }
}
When you log objects with protected properties, they'll automatically be redacted:
var user = new User
{
Name = "John Doe",
Password = "secret123",
ApiKey = "sk_live_abc123"
};
logger.LogInformation("User details: {@User}", user);
// Output: User details: { Name: "John Doe", Password: "****", ApiKey: "****" }
[ProtectedDataClassification] attribute to mark sensitive properties****AddStarRedactor()Registers the StarRedactor and enables redaction in the logging pipeline.
services.AddStarRedactor();
ProtectedDataClassificationAttributeMarks a property as containing protected data that should be redacted.
[ProtectedDataClassification]
public string SensitiveData { get; set; }
StarRedactorThe default redactor that replaces sensitive values with ****.
DefaultDataClassificationsProvides standard data classification types:
Protected: Data that should be redactedPublic: Data that can be logged without redactionYou can use the built-in classifications for more granular control:
using Microsoft.Extensions.Compliance.Classification;
public class CustomModel
{
[DataClassification(DefaultDataClassifications.Public)]
public string PublicInfo { get; set; }
[DataClassification(DefaultDataClassifications.Protected)]
public string PrivateInfo { get; set; }
}
For more information about data redaction in .NET, see the official documentation:
MIT