【问题标题】:ASP.Net MVC: Where the user role is checking against supplied rolesASP.Net MVC:用户角色正在检查提供的角色
【发布时间】: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 是否具有经理角色的代码中完成此检查?

谢谢

【问题讨论】:

    标签: asp.net-mvc asp.net-roles


    【解决方案1】:

    注意他们的AuthLogAttribute 继承自AuthorizeAttribute,然后在OnAuthorization 上,他们只是调用AuthorizeAttribute.OnAuthorization(filterContext);

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);
        IsUserAuthorized(filterContext);
    }
    

    在调用之后,他们会调用自己的IsUserAuthorized(),它会检查filterContextResult 属性是否由基础AuthorizeAttribute 设置。如果不为 null 且用户已通过身份验证,则表示该用户没有所需的角色。

    因此,要回答您关于“在哪里比较 user1 在代码中没有称为 Manager 的角色”的问题,这将是基础 AuthorizeAttribute

    我个人认为AuthLogAttribute 没有什么价值,因为它只是指定默认错误消息以及如果用户不是所需角色时要转到的视图。我会坚持使用标准的AuthorizeAttribute

    【讨论】:

    • 抱歉,目前尚不清楚系统如何检测 user1 是否具有称为 manager 的角色。如果 user1 没有名为 manager 的角色,则重定向到错误页面。如果可能,请发布一个小的工作代码。
    • 查看 AuthorizeAttribute 的源代码。具体来说,第 102 行:github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/… 它正在检查用户是否具有传递给属性的任何角色
    • 感谢您的链接,但下面这些行不清楚if (_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) { return false; } if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)) { return false; }请帮助我理解。
    • 别担心_usersSplit。您只使用角色..._rolesSplit 是一个包含所需角色的列表。在您的示例中,您为Create() 指定“经理”,为SaleProduct 指定“销售主管”。重要的一行是_rolesSplit.Any(user.IsInRole))。它为_rolesSplit 中的每个角色从主体调用IsInRole(string) 方法,以查看用户是否具有该角色。如果 `IsInRoleIsInRole 没有为任何所需角色返回 true,那么它将返回 false。
    • 如何从 VS2013 IDE 调试 .net 代码。我如何调试 dotnet 程序集中的IsAuthorized function 的代码?
    猜你喜欢
    • 2019-08-22
    • 1970-01-01
    • 2013-11-10
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 2013-03-31
    • 1970-01-01
    相关资源
    最近更新 更多