自定义角色管理器/提供程序 [英] Custom Role Manager / Provider

查看:129
本文介绍了自定义角色管理器/提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下在MVC5应用程序中实现自定义角色管理器自定义角色提供者教程.

我已经创建了我的自定义角色提供程序,它覆盖了2个必需的功能.

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)
    {

然后我需要更改我的Web配置以使用此新提供程序...

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:

解析器错误消息:无法加载类型'Models.Security.CustomRoleProvider'.

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>

据我所知,我已经完成了所有必需的工作.唯一不同的是我正在使用链接到AD的自定义成员身份

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>

还有其他需要做的事情吗? MVC5是否改变了方法?

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

推荐答案

您仍然可以使用roleManager和自定义角色提供程序.只需将您的主体设置为RolePrincipal.您可以在IAuthenticationFilter上执行此操作,然后在global.asax上注册.请参阅这两个链接以获取一些示例代码: RolePrincipal / IAuthenticationFilter

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

这篇关于自定义角色管理器/提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆