我有这段代码,但我不知道为什么它不能正常工作。我创建了一个自定义角色提供程序。
@if (Roles.IsUserInRole(User.Identity.Name, "Manager"))
{
<li>@Html.ActionLink("User Management", "Index", "User")</li>
}
这是自定义代码,其余未修改。
public override bool IsUserInRole(string username, string roleName)
{
UserRoleType usrt = (from usr in db.Users
join usrRole in db.UserRoles on usr.UserID equals usrRole.UserID
where usr.Email == username
select usrRole.UserRoleType).FirstOrDefault();
if (roleName.Split(',').Contains(usrt.UserRoleTypeName))
return true;
return false;
}
当我这样做时,这确实有效:
UserRoleProvider roleProvider = System.Web.Security.Roles.Provider as UserRoleProvider;
if (roleProvider.IsUserInRole(httpContext.User.Identity.Name, Roles) || String.IsNullOrEmpty(Roles))
return true;
编辑:
public override string[] GetRolesForUser(string roleName)
{
return db.UserRoleTypes.Select(u => u.UserRoleTypeName).ToArray();
}