MVC自定义角色提供程序不打 [英] MVC Custom Role Provider not being hit

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

问题描述

我说不管什么原因它不使用自定义角色提供但,好像是继续得到使用默认值。这里是我的code自定义角色提供

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

我的控制器

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

和的Web.Config

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>

如果我设置在我CustomRoleProvider断点,它从来没有被击中......

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

任何想法?

推荐答案

是在解决方案中的一个单独的项目自定义提供code和加入到解决方案中的MVC项目的参考?如果是这样,你可能需要有这样的:

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" />

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

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