我添加了一个自定义角色提供程序,但由于某种原因它并未被使用,似乎默认的角色提供程序仍在使用。以下是我的自定义角色提供程序代码:
如果我在CustomRoleProvider中设置断点,它从未被触发……有什么想法吗?
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中设置断点,它从未被触发……有什么想法吗?