Skip to content

Instantly share code, notes, and snippets.

@conwid
Created May 16, 2023 16:49
Show Gist options
  • Save conwid/0e15520470a70e46af6f0757518a4303 to your computer and use it in GitHub Desktop.
Save conwid/0e15520470a70e46af6f0757518a4303 to your computer and use it in GitHub Desktop.
Demos showcasing data protection capabilities of ASP.NET Identity Core
public class CustomLookupProtector : ILookupProtector
{
[return: NotNullIfNotNull("data")]
public string? Protect(string keyId, string? data)
{
return data == null ? null : new string(data.Reverse().ToArray());
}
[return: NotNullIfNotNull("data")]
public string? Unprotect(string keyId, string? data)
{
return data == null ? null : new string(data.Reverse().ToArray());
}
}
public class CustomKeyRing : ILookupProtectorKeyRing
{
private string currentKeyId = "1";
public string this[string keyId]
{
get
{
if (keyId == "1")
return "A";
return "B";
}
}
public string CurrentKeyId
{
get
{
return currentKeyId;
}
}
public IEnumerable<string> GetAllKeyIds()
{
return new List<string> { "1", "2" };
}
}
public class ApplicationUser : IdentityUser
{
public DateTime DateOfBirth { get; set; }
[PersonalData,ProtectedPersonalData]
public string Address { get; set; }
}
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(opts => opts.Stores.ProtectPersonalData = true)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddClaimsPrincipalFactory<CustomClaimFactory>()
.AddPersonalDataProtection<CustomLookupProtector, CustomKeyRing>();
public class CustomPersonalDataProtector : DefaultPersonalDataProtector
{
public CustomPersonalDataProtector(ILookupProtectorKeyRing keyRing, ILookupProtector protector) : base(keyRing, protector)
{
}
public override string? Protect(string? data)
{
var x = base.Protect(data);
return x;
}
public override string? Unprotect(string? data)
{
var x = base.Unprotect(data);
return x;
}
}
//builder.Services.AddScoped<IPersonalDataProtector, MyPersonalDataProtector>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment