【发布时间】:2016-09-22 00:00:33
【问题描述】:
我正在尝试获取基于角色和声明的身份验证。
刚刚阅读这篇文章http://www.dotnetcurry.com/aspnet-mvc/1102/aspnet-mvc-role-based-security
假设用户登录并且他具有销售角色但他尝试访问管理员角色的操作但我发现没有代码可以比较用户角色和提供的角色来知道用户是否具有管理员角色。看到这个区域
private void IsUserAuthorized(AuthorizationContext filterContext)
{
// If the Result returns null then the user is Authorized
if (filterContext.Result == null)
return;
//If the user is Un-Authorized then Navigate to Auth Failed View
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
// var result = new ViewResult { ViewName = View };
var vr = new ViewResult();
vr.ViewName = View;
ViewDataDictionary dict = new ViewDataDictionary();
dict.Add("Message", "Sorry you are not Authorized to Perform this Action");
vr.ViewData = dict;
var result = vr;
filterContext.Result = result;
}
}
[AuthLog(Roles = "Manager")]
public ActionResult Create()
{
var Product = new ProductMaster();
return View(Product);
}
[AuthLog(Roles = "Sales Executive")]
public ActionResult SaleProduct()
{
ViewBag.Message = "This View is designed for the Sales Executive to Sale Product.";
return View();
}
假设 user1 角色为 "Sales Executive" 尝试访问 Create action,然后比较 user1 strong> 在代码中没有称为 Manager 的角色吗? 这是怎么回事?谁在幕后做事?
请帮助我理解这一点,只有在 user1 是否具有经理角色的代码中完成此检查?
谢谢
【问题讨论】: