MVC Custom Role Provider not being hit

安稳与你 提交于 2020-01-02 07:51:36

问题


I added a custom role provider but for whatever reason its not being used, it seems like the default is keep getting used. Here is my code for custom role provider

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;
            }
        }
    }
}

My Controller

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

And Web.Config

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

If I set a breakpoint in my CustomRoleProvider, it never gets hit...

Any Ideas ?


回答1:


Is the custom provider code in a separate project in your solution and added as a reference to the MVC project in your solution? If so, you may need to have something like:

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


来源:https://stackoverflow.com/questions/23105321/mvc-custom-role-provider-not-being-hit

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