我添加了一个自定义角色提供程序,但由于某种原因未使用它,似乎默认设置是继续使用。这是我的自定义角色提供程序的代码

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中设置一个断点,它将永远不会被击中...

有任何想法吗 ?

最佳答案

定制提供程序代码是否在您的解决方案的一个单独项目中,并作为对您解决方案中的MVC项目的引用而添加?如果是这样,您可能需要准备以下内容:

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

关于c# - MVC自定义角色提供程序未受到攻击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23105321/

10-10 00:01