MVC自定义角色提供程序未被命中

时间:2014-04-16 09:30:12

标签: c# asp.net-mvc windows-authentication roleprovider

我添加了一个自定义角色提供程序,但无论出于何种原因它没有被使用,似乎默认是继续使用。这是我的自定义角色提供程序的代码

namespace Models.Security
{
    public class MatchMakerRoleProvider: RoleProvider
    {
        public override string[] GetRolesForUser(string username)
        {
            username = username.Split('\\')[1].ToLower();
            using (var db = new EncodingEntities())
            {
                var user = db.Admin_Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase));


                    var roles = from ur in user.Admin_UserRoles
                        from r in db.Admin_Roles
                        where ur.RoleId == r.RoleId
                        select r.RoleName;
                    if (roles != null)
                        return roles.ToArray();
                    else
                        return new string[] {};
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            using (var db = new EncodingEntities())
            {
                var user = db.Admin_Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase));

                var roles = from ur in user.Admin_UserRoles
                            from r in db.Admin_Roles
                            where ur.RoleId == r.RoleId
                            select r.RoleName;
                if (user != null)
                    return roles.Any(r => r.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                else
                    return false;
            }
        }
    }
}

我的控制器

[Authorize(Roles = "SuperAdmin")]
public class AdminController : Controller

和Web.Config

<authentication mode="Windows">
</authentication>
<roleManager enabled="true" defaultProvider="MatchMakerRoleProvider" cacheRolesInCookie="true">
  <providers>
    <clear/>
    <add name="MatchMakerRoleProvider" type="Models.Security.MatchMakerRoleProvider" />
  </providers>
</roleManager>

如果我在CustomRoleProvider中设置断点,它永远不会被击中......

任何想法?

1 个答案:

答案 0 :(得分:1)

自定义提供程序代码是否位于解决方案中的单独项目中,并作为对解决方案中MVC项目的引用添加?如果是这样,您可能需要具有以下内容:

<add name="CustomRoleProvider" type="Namespace.For.CustomRoleProvider, Your.Assembly.Name" />