title: ASP.Net Core 2.1 register custom ClaimsPrincipal
body: <p>I am creating a Windows Authentication app but the roles sit within the custom database and not on the AD so I created a custom ClaimsPrincipal to override the User.IsInRole() function that usually looks at the AD for roles.</p>
  
  <p>However, when running the application it still seems to be using the original code and not my CustomClaimsPrincipal. 
  I get the error "The trust relationship between the primary domain and the trusted domain failed".</p>
  
  <p>In ASP.Net MVC 5 I used a Custom RoleProvider which is essentially what I am trying to replicate here.</p>
  
  <p><strong>CustomClaimsPrincipal.cs</strong> </p>
  
  <pre><code>public class CustomClaimsPrincipal : ClaimsPrincipal
  {
      private readonly ApplicationDbContext _context;
  
      public CustomClaimsPrincipal(ApplicationDbContext context)
      {
          _context = context;
      }
  
      public override bool IsInRole(string role)
      {
          var currentUser = ClaimsPrincipal.Current.Identity.Name;
  
          IdentityUser user = _context.Users.FirstOrDefault(u =&gt; u.UserName.Equals(currentUser, StringComparison.CurrentCultureIgnoreCase));
  
          var roles = from ur in _context.UserRoles.Where(p =&gt; p.UserId == user.Id)
                      from r in _context.Roles
                      where ur.RoleId == r.Id
                      select r.Name;
          if (user != null)
              return roles.Any(r =&gt; r.Equals(role, StringComparison.CurrentCultureIgnoreCase));
          else
              return false;
      }
  }
  </code></pre>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>        services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;().AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();
  
          services.AddScoped&lt;ClaimsPrincipal,CustomClaimsPrincipal&gt;();
  </code></pre>
  
  <p>Not sure if the above code in Startup.cs is the correct way to override the ClaimsPrincipal as I'm new to the .Net Core framework.</p>