And*_*lli 5 asp.net-identity .net-core asp.net-core .net-5
我找到了 ASP.NET Core Identity 框架的属性类ProtectedPersonalData(链接),但我似乎找不到任何有关如何使用它的文档。文档只说:Used to indicate that a something is considered personal data and should be protected.
最后,我能够加密身份用户类字段(链接)(例如电子邮件字段),但不能加密身份用户继承类的任何属性。
public class ApplicationUser : IdentityUser {
[ProtectedPersonalData]
public string MyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我将其添加到身份配置中:
services.AddDefaultIdentity<ApplicationUser>(options => {
options.Stores.ProtectPersonalData = true;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)
此外,我实现了保护器类:
public class Lookup : ILookupProtector {
public string Protect(string keyId, string data) {
return new string(data?.Reverse().ToArray());
}
public string Unprotect(string keyId, string data) {
return new string(data?.Reverse().ToArray());
}
}
public class Protector : IPersonalDataProtector {
public string Protect(string data) {
return new string(data?.Reverse().ToArray());
}
public string Unprotect(string data) {
return new string(data?.Reverse().ToArray());
}
}
public class KeyRing : ILookupProtectorKeyRing {
public string this[string keyId] => "key";
public string CurrentKeyId => "key";
public IEnumerable<string> GetAllKeyIds() {
return new string[] { "key" };
}
}
Run Code Online (Sandbox Code Playgroud)
可以加密MyProperty字段吗?请给我指出信息或提供一些例子。
更新:
我注意到代码永远不会进入Protectproperty 的方法内部MyProperty。
您需要将数据注释添加到符合PersonalData资格的属性,如下所示:
[ProtectedPersonalData]
[PersonalData]
public string Firstname { get; set; }
[ProtectedPersonalData]
[PersonalData]
public string Lastname { get; set; }
Run Code Online (Sandbox Code Playgroud)
为了激活该过程,您需要在Startup.cs中注册服务:
// ProtectedData
services.AddScoped<ILookupProtectorKeyRing, KeyRing>();
services.AddScoped<ILookupProtector, LookupProtector>();
services.AddScoped<IPersonalDataProtector, PersonalDataProtector>();
Run Code Online (Sandbox Code Playgroud)
在这里,您可以找到一个示例存储库,其中包含带有 Microsoft Identity 帐户和ProtectedData实现的 Blazor WASM 项目。
https://github.com/nbiada/protecteddata-wasm-example
| 归档时间: |
|
| 查看次数: |
2845 次 |
| 最近记录: |