How can UserName’s ProtectedPersonalData attribute be overridden in ASP.NET Core Identity?

I’m using ASP.NET Core Identity in a Blazor project. I’d like to encrypt the user’s personal data, however, I don’t want to encrypt the UserName. My plan is to use the UserName as an account id rather than the user’s name. The user’s name will instead be held externally to the IdentityUser class and encrypted separately using a different encryption mechanism. This account id, which will be held in UserName, needs to be memorable, but not contain user identifiable data, so won’t need to be encrypted, it will just be like any normal login id. My hope is that, by not encrypting this account id, it will be possible for the user to recover their account under certain scenarios.

I’ve been unable to override the ProtectedPersonalData attribute on UserName.

I’ve added a LookupProtector class that realises ILookupProtector and LookupProtectorKeyRing that realises ILookupProtectorKeyRing.

I’ve added a class called ApplicationUser that inherits from IdentityUser and given it a UserName property without the ProtectedPersonalData attribute …

public class ApplicationUser : IdentityUser
{
    /// <summary>
    /// Gets or sets the user name for this user.
    /// </summary>
    public override string UserName { get; set; } = null!;
}

I’ve modified ApplicationDbContext to use ApplicationUser as a generic type …

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

I’ve modified the call to AddDefaultIdentity to refer to ApplicationUser instead of IdentityUser and added a call to AddPersonalDataProtection with generic types for LookupProtector and LookupProtectorKeyRing

builder.Services.AddDefaultIdentity<ApplicationUser>(options =>
{
    options.SignIn.RequireConfirmedAccount = true;
    options.User.RequireUniqueEmail = true;
    options.Stores.ProtectPersonalData = true;
}).AddRoles<IdentityRole>()
  .AddEntityFrameworkStores<ApplicationDbContext>()
  .AddPersonalDataProtection<LookupProtector, LookupProtectorKeyRing>();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<ApplicationUser>>();

I’ve replaced all other references to IdentityUser in my code to ApplicationUser, for example, the instances of UserManager and SignInManager use ApplicationUser as their generic type instead of IdentityUser

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

I’ve tried scaffolding the User class in case that does something different to what I’ve done manually. I needed to click ‘+’ then type in the name of the ApplicationDbContext by hand and also check an unwanted file ‘Account/Lockout’ to get it to proceed, but even then it failed with a build error, even though my code builds fine with a normal build. The build error was, “There was an error running the selected code generator: Failed to compile the project in memory”
Scaffold settings

I’ve also dropped the existing database, rolled back all migrations (including the original Identity migration) and then added a single new migration for all models created to-date using these commands …

dotnet ef database drop
dotnet ef migrations remove (multiple calls)
dotnet ef migrations add CreateTables
dotnet ef database update

However, now when I register a user I see that the method LookupProtector.Protect is called with data that seems to be from the UserName or NormalizedUserName.

Am I missing something here? Please, let me know.

I could try to work around this by adding a UserId property to ApplicationUser and attempt to leave the UserName mostly ignored, but I’d like to find a solution that feels more ‘correct’ if I can.

New contributor

Rick P. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật