问题
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