title: ASP.NET Core 2.1 Custom RoleProvider with Windows Authentication
body: <p>I am migrating applications away from the ASP.Net MVC 5 framework to the new .Net Core 2.1.</p>
  
  <p>I used Windows Authentication with a Custom RoleProvider in the MVC 5 Projects as shown in the link below.</p>
  
  <p><a href="https://stackoverflow.com/questions/41940905/asp-net-mvc-how-to-create-a-custom-role-provider">ASP.NET MVC How to create a custom role provider</a></p>
  
  <p>How do I accomplish the same in Core 2.1 as it does not seem to contain RoleProvider capability?</p>
  
  <p>Every example I come across uses Individual Accounts with IdentityUser and IdentityRole.</p>
  
  <p>My custom tables for User and Roles :</p>
  
  <pre><code>public class User
  {
      public User() { UserRoles = new HashSet&lt;UserRole&gt;(); }
  
      [Key]
      public string Id { get; set; }
  
      [StringLength(50)]
      [Required]
      public string Logon { get; set; } //The users Active Directory Username
  
      public bool Active { get; set; }
  
      public ICollection&lt;UserRole&gt; UserRoles { get; set; }
  
  }
  
  
  public class Role
  {
      public Role() { UserRoles = new HashSet&lt;UserRole&gt;(); }
  
      [Key]
      public string Id { get; set; }
  
      public string Name { get; set; }
  
      public ICollection&lt;UserRole&gt; UserRoles { get; set; }
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>I've added a CustomClaimsPrincipal which goes like: </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));
              //(ApplicationUser)_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>and added to Startup.cs</p>
  
  <pre><code>services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;().AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();
  
  services.AddScoped&lt;ClaimsPrincipal, CustomClaimsPrincipal&gt;();
  </code></pre>
  
  <p>But it still seems to be taking the original ClaimsPrincipal IsInRole function instead of the override which I believe is why I'm getting the error message "The trust relationship between the primary domain and the trusted domain failed."</p>