Custom Role Manager / Provider

我的梦境 提交于 2019-12-25 04:59:15

问题


I am trying to implement a custom role manager in an MVC5 application using the following Custom Role Provider Tutorial.

I have created my Custom Role Provider overiding the 2 required functions.

namespace Models.Security
{
public class CustomRoleProvider : RoleProvider
{
/// logic
 public override string[] GetRolesForUser(string username)
    {
/// logic
public override bool IsUserInRole(string username, string roleName)
    {

I then need to alter my web config to use this new provider...

<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
 <roleManager enabled="true" defaultProvider="CustomRoleProvider">
  <providers>
    <clear />
    <add name="CustomRoleProvider" type="Models.Security.CustomRoleProvider"/>
  </providers>
</roleManager>

However when I try and access my application I get the following error:

Parser Error Message: Could not load type 'Models.Security.CustomRoleProvider'.

Source Error:

Line 29:       <providers>
Line 30:         <clear />
Line 31:         <add name="CustomRoleProvider" type="Models.Security.CustomRoleProvider"/>
Line 32:       </providers>
Line 33:     </roleManager>

As far as I am aware I have done everything that is required. The only thing that is different is I am using a custom membership linking to AD

<authentication mode="Forms">
  <forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="200" slidingExpiration="false" protection="All" />
</authentication>
<membership defaultProvider="ADMembershipProvider">
  <providers>
    <clear />
    <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
  </providers>
</membership>

Is there something else I need to do? Has MVC5 changed the approach?


回答1:


Yes. MVC 5 uses Identity, Role providers are an ASP.NET Membership concept. You don't need role providers in Identity, because you control the roles. It's an entity in your project (or at least it can be if you subclass IdentityRole), so you can do whatever you want.




回答2:


You can still use roleManager and your custom role provider. Just have to set your principal to type RolePrincipal. You can do that on IAuthenticationFilter and register it on global.asax. See those two links for some sample code: RolePrincipal / IAuthenticationFilter



来源:https://stackoverflow.com/questions/36499821/custom-role-manager-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!