title: Clean Architecture Issue when using .Net Core Identity (.NET 5)
body: <p>I've recently started messing with the Clean Architecture concept while using .NET 5. The problem I am running into is that although Identity is well isolated in the Infrastructure layer, if I want to use additional properties on the user without mixing that data with the AspNetUsers table, the problems start.</p>
  <p>I will have to create a new entity in the Domain layer, for example UserProfile, and figure out a way to relate that entity to the ApplicationUser entity without</p>
  <ol>
  <li>including an ApplicationUser reference in my new entity</li>
  <li>moving the ApplicationUser entity to the Domain layer and reference Identity namespaces in there.</li>
  </ol>
  <p>In all Clean Architecture projects this situation is either not addressed (they simply add new properties into ApplicationUser) or the solution given is to include a reference to the Identity User Id in the new entity.</p>
  <p>I don't like the first solution since it forces me to add custom fields to an Identity table. The second solution brings new and complex problems since it will force me to manually synchronize 2 tables in case of creation or deletion of data, also forcing the use of transactions to avoid orphan data in case of synchronization failure. It also brings an abstraction issue since deleting a user using only Identity will not take care automatically of unknown entities (from future plugins) that might depend or relate somehow to AspNetUsers table.</p>
  <p>Let me be more concrete. Here's my simplified project structure (using .NET core 5):</p>
  <p><strong>Application</strong></p>
  <ul>
  <li>Interface
  <ul>
  <li>IApplicationDbContext.cs</li>
  </ul>
  </li>
  </ul>
  <p><strong>Core</strong></p>
  <ul>
  <li>Domain
  <ul>
  <li>UserProfile.cs (my custom user class with additional properties)</li>
  </ul>
  </li>
  </ul>
  <p><strong>Infrastructure</strong></p>
  <ul>
  <li>Data
  <ul>
  <li>ApplicationDbContext.cs (Interface implementation)</li>
  </ul>
  </li>
  <li>Identity
  <ul>
  <li>ApplicationRole.cs</li>
  <li>ApplicationUser.cs
  DependencyInjection.cs</li>
  </ul>
  </li>
  </ul>
  <p><strong>Web</strong></p>
  <p>my web application</p>
  <p>Here's my code:</p>
  <p><strong>IApplicationDbContext.cs</strong></p>
  <pre><code>namespace Application.Common.Interfaces
  {
      public interface IApplicationDbContext
      {        
          Task&lt;int&gt; SaveChangesAsync(CancellationToken cancellationToken);
      }
  }
  </code></pre>
  <p><strong>UserProfile.cs</strong></p>
  <pre><code>namespace Core.Domain
  {
      public class UserProfile : AuditEntity
      {
          // Don't want to reference ApplicationUser in domain.
          // [ForeignKey(&quot;Id&quot;)]
          //public virtual ApplicationUser User { get; set; }
  
          [StringLength(50)]
          public string FirstName { get; set; }
  
          [StringLength(50)]
          public string LastName { get; set; }
  
          [StringLength(50)]
          public string JobPosition { get; set; }
  
          [StringLength(50)]
          public string Photo { get; set; }
  
          [DefaultValue(true)]
          public bool IsListed { get; set; }
  
          [DefaultValue(false)]
          public bool IsDisabled { get; set; }
  
          [DefaultValue(false)]
          public bool IsLocked { get; set; }
  
          public DateTime? LastLoginDate { get; set; }
  
          public DateTime? LastActivityDate { get; set; }
  
          [StringLength(100)]
          public string LastSessionId { get; set; }
  
          [StringLength(256)]
          public string PrivateFolder { get; set; }
  
          public bool IsOnApproval { get; set; } = true;
  
          public bool IsDeleted { get; set; } = false;
      }
  }
  </code></pre>
  <p><strong>ApplicationDbContext.cs</strong></p>
  <pre><code>namespace Infrastructure.Data
  {
      
      public partial class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser, ApplicationRole, int&gt;, IApplicationDbContext
      {                        
          public ApplicationDbContext(DbContextOptions options) : base(options)
          {
              
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
  
  
              modelBuilder.ApplyConfiguration(new Configurations.UserConfiguration());                    
              modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());                            
          }
                     
          public override Task&lt;int&gt; SaveChangesAsync(CancellationToken cancellationToken)
          {
              var entries = ChangeTracker.Entries().Where(x =&gt; x.Entity is AuditEntity &amp;&amp; (x.State == EntityState.Added || x.State == EntityState.Modified));
  
              foreach (var entry in entries)
              {
                  if (entry.State == EntityState.Added)
                  {
                      ((AuditEntity)entry.Entity).CreatedBy = _currentUserService.UserId;
                      ((AuditEntity)entry.Entity).CreatedDate = DateTime.UtcNow;
                  }
                  ((AuditEntity)entry.Entity).LastModifiedBy = _currentUserService.UserId;
                  ((AuditEntity)entry.Entity).LastModifiedDate = DateTime.UtcNow;
              }
              return base.SaveChangesAsync(cancellationToken);
          }
  
      }
  }
  </code></pre>
  <p><strong>ApplicationRole.cs</strong></p>
  <pre><code>namespace Infrastructure.Identity
  {
      
      public class ApplicationRole : IdentityRole&lt;int&gt; //, ISoftDeletable
      {
          public ApplicationRole() : base() { }
          public ApplicationRole(string name) : base(name) { }
      }
  }
  </code></pre>
  <p><strong>ApplicationUser.cs</strong></p>
  <pre><code>namespace Infrastructure.Identity
  {    
      public class ApplicationUser: IdentityUser&lt;int&gt;
      {      
          
      }
  }
  </code></pre>
  <p><strong>DependencyInjection.cs</strong></p>
  <pre><code>namespace Infrastructure
  {
      public static class DependencyInjection
      {
          public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration config)
          {            
              services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  options.UseSqlServer(
                      config.GetConnectionString(&quot;eCMConnection&quot;),
                      context =&gt; context.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName)));
           
              services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;(
                      options =&gt;
                      {
                          options.Password.RequireDigit = true;
                          options.Password.RequiredLength = 8;
                          options.Password.RequireNonAlphanumeric = true;
                          options.SignIn.RequireConfirmedAccount = false;
                      }
                  )
                  .AddRoleManager&lt;RoleManager&lt;ApplicationRole&gt;&gt;()
                  .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
                  .AddDefaultTokenProviders(); // two factor authentication
              
              services.AddScoped&lt;IApplicationDbContext&gt;(provider =&gt; provider.GetService&lt;ApplicationDbContext&gt;());
  
              services.AddTransient&lt;IEmailService, EmailService&gt;();
  
              return services;
          }
      }
  }
  </code></pre>
  <p>So, my question is, what is the best approach to be able to have a custom UserProfile entity with data stored in a table related to AspNetUsers table without having to reference ApplicationUser and Identity in UserProfile domain entity. What is the best approach to be able to create and delete Identity users but also related data from my UserProfile table. As anyone seen an example of this implemented?</p>
  <p>Thanks! Hope someone can throw some light into this.</p>