Created
May 16, 2023 16:49
-
-
Save conwid/0e15520470a70e46af6f0757518a4303 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Demos showcasing data protection capabilities of ASP.NET Identity Core |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApplicationUser : IdentityUser | |
{ | |
public DateTime DateOfBirth { get; set; } | |
[PersonalData,ProtectedPersonalData] | |
public string Address { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(opts => opts.Stores.ProtectPersonalData = true) | |
.AddEntityFrameworkStores<ApplicationDbContext>() | |
.AddClaimsPrincipalFactory<CustomClaimFactory>() | |
.AddPersonalDataProtection<CustomLookupProtector, CustomKeyRing>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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