0

I have a centralized membership database that stores roles for different MVC applications. In other MVC applications, I can just put the below snippet in web.config to associate roles to the User object and then I can just use “User.IsInRole(Role.Admin)” to check a user’s role.

<roleManager defaultProvider="SqlProvider" 
      enabled="true"
      cacheRolesInCookie="true"
      cookieName=".ASPROLES"
      cookieTimeout="30"
      cookiePath="/"
      cookieRequireSSL="true"
      cookieSlidingExpiration="true"
      cookieProtection="All" >
      <providers>
        <add
          name="SqlProvider"
          type="System.Web.Security.SqlRoleProvider"
          connectionStringName="SqlServices" 
          applicationName="SampleApplication" />
      </providers>
    </roleManager>

The above method doesn’t seem to work in .NET core. The role provider that I declared is getting ignored. How do I use SqlRoleProvider in .NET core?

Note: this application doesn't actually create any user/role. It just needs to read from that membership database and authorize users based on their roles.

John Smith
  • 29
  • 3
  • which version asp.net core 1.0 or 2.0? – Ajay2707 Jul 30 '18 at 18:11
  • The version I'm using is 2.0. – John Smith Jul 30 '18 at 18:13
  • See [Custom RoleProvider in ASP.NET Core with Identity?](https://stackoverflow.com/questions/35996498/custom-roleprovider-in-asp-net-core-with-identity) – Mark G Jul 30 '18 at 22:01
  • That solution probably would work for small/personal projects but I still want to use that centralized database for authentication to keep things consistent with other MVC projects. – John Smith Jul 30 '18 at 22:52
  • @GuanHuang If you want to integrate into Identity then take a look at [Dynamic Claims in ASP.NET Core](http://www.spaprogrammer.com/2016/02/dynamic-claims-in-aspnet-core-10.html), but the same principle applies. – Mark G Jul 31 '18 at 01:22
  • Thank you, Mark. That might be my only option now. It used to be so simple in MVC and now I have to write more code to accomplish the same thing in CORE. – John Smith Jul 31 '18 at 22:49

0 Answers0