我正在尝试创建自己的 [Authorize] 属性,以便我可以使用自己的授权逻辑来拥有分层角色。
如果有人在 Controller 或操作上执行[Authorize(Roles = "Admin")]
如何在我的 AuthorizeCore 函数中获取字符串“Admin”?
我正在使用此代码:
public class Authorize : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//authorize role logic
if (true)
return true;
return false;
}
}
MVC4、.net 4.5、c#、VS 2012
最佳答案
这对你来说是很常见的事情。
帖子中的此建议应该适用于 MVC4,因为它适用于 MVC 3:- ASP.NET MVC - Alternative to Role Provider?
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool isAdmin;
if(Roles.Contains("Admin"))
isAdmin = true;
return isAdmin ;
}
关于asp.net-mvc - MVC4 授权属性覆盖;如何通过角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13868801/