امکان رمزنگاری اطلاعات شخصی کاربران در ASP.NET Core Identity 2.1
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۰۶/۱۱ ۱۲:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class IdentityUser<TKey> where TKey : IEquatable<TKey>
{
[ProtectedPersonalData]
public virtual string UserName { get; set; }
[ProtectedPersonalData]
public virtual string Email { get; set; } services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Stores.ProtectPersonalData = true;
// ...
}) private class PersonalDataConverter : ValueConverter<string, string>
{
public PersonalDataConverter(IPersonalDataProtector protector) :
base(s => protector.Protect(s), s => protector.Unprotect(s), default)
{ }
} protected override void OnModelCreating(ModelBuilder builder)
{
var encryptPersonalData = storeOptions?.ProtectPersonalData ?? false;
builder.Entity<TUser>(b =>
{
if (encryptPersonalData)
{
converter = new PersonalDataConverter(this.GetService<IPersonalDataProtector>());
var personalDataProps = typeof(TUser).GetProperties().Where(
prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
foreach (var p in personalDataProps)
{
if (p.PropertyType != typeof(string))
{
throw new InvalidOperationException(Resources.CanOnlyProtectStrings);
}
b.Property(typeof(string), p.Name).HasConversion(converter);
}
}
// ... public interface IPersonalDataProtector
{
string Protect(string data);
string Unprotect(string data);
} if (Options.Stores.ProtectPersonalData)
{
if (!(Store is IProtectedUserStore<TUser>))
{
throw new InvalidOperationException(Resources.StoreNotIProtectedUserStore);
}
if (services.GetService<ILookupProtector>() == null)
{
throw new InvalidOperationException(Resources.NoPersonalDataProtector);
}
} public class UserStore<TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRoleClaim> : ...
public virtual async Task UpdateNormalizedUserNameAsync(TUser user)
{
var normalizedName = NormalizeName(await GetUserNameAsync(user));
normalizedName = ProtectPersonalData(normalizedName);
await Store.SetNormalizedUserNameAsync(user, normalizedName, CancellationToken);
}