Skip to content

Implement IProtectedUserStore for automatic personal data encryption #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 11, 2025

This PR implements the IProtectedUserStore functionality requested in the TODO comment by adding automatic encryption/decryption for user personal data marked with [ProtectedPersonalData].

Changes Made

1. PersonalDataProtectionService

  • Created Services/PersonalDataProtectionService.cs implementing IPersonalDataProtector
  • Uses ASP.NET Core Data Protection API for secure encryption/decryption
  • Includes backward compatibility for existing unencrypted data
  • Handles null/empty values gracefully

2. Identity Configuration Updates

  • Enabled options.Stores.ProtectPersonalData = true in Identity setup
  • Added Data Protection services registration
  • Registered PersonalDataProtectionService as IPersonalDataProtector implementation
  • Removed the TODO comment indicating completion

3. Automatic Protection for Existing Fields

The existing user model already has the required attributes:

public class EssentialCSharpWebUser : IdentityUser
{
    [ProtectedPersonalData]  // Will now be automatically encrypted
    public virtual string? FirstName { get; set; }
    
    [ProtectedPersonalData]  // Will now be automatically encrypted  
    public virtual string? LastName { get; set; }
    
    public int ReferralCount { get; set; }  // Not encrypted
}

How It Works

  • Encryption: When user data is saved, properties with [ProtectedPersonalData] are automatically encrypted
  • Decryption: When user data is loaded, encrypted properties are automatically decrypted
  • Backward Compatibility: Existing unencrypted data continues to work unchanged
  • Security: Uses ASP.NET Core's Data Protection API with proper key management and rotation

Testing

Added comprehensive tests covering:

  • Encryption/decryption functionality
  • Null and empty string handling
  • Special character support
  • Backward compatibility with existing data
  • Integration with the existing user model

Migration Impact

  • No database migration required
  • No breaking changes
  • Existing unencrypted data continues to work
  • New data will be automatically encrypted

Fixes #251.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits July 11, 2025 06:02
Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
…ection

Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Consider implementing IProtectedUserStore Implement IProtectedUserStore for automatic personal data encryption Jul 11, 2025
@Copilot Copilot AI requested a review from BenjaminMichaelis July 11, 2025 06:10
Copilot finished work on behalf of BenjaminMichaelis July 11, 2025 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider implementing IProtectedUserStore
2 participants