“授权”可单独使用,但不能与角色一起使用

时间:2019-04-20 13:43:21

标签: c# asp.net-core

我正在构建的应用程序需要几个角色,并且最初似乎运行良好:我可以使用“ User1”登录,并且在首次启动时,“ IsRole_ModuleAll_Admin”将返回true:

    IdentityUser MyUser = UserManager.Users.FirstOrDefault();
    IList<string> MyRoles = await UserManager.GetRolesAsync(MyUser);


    bool IsRole_ModuleAll_Admin = MyRoles.Contains("Module: All - Admin"); //True
    bool IsRole_ModuleStatus_TechnicianOSC = MyRoles.Contains("Module: Status - TechnicianOSC");
    bool IsRole_ModuleStatus_Admin = MyRoles.Contains("Module: Status - Admin");

    if (IsRole_ModuleAll_Admin || IsRole_ModuleStatus_TechnicianOSC)
    {
        <li>
            <a href="#StatusSubmenu" data-toggle="collapse" aria-expanded="false" aria-controls="PlannerSubmenu"><i class="fas fa-satellite-dish fa-fw"></i>&nbsp;&nbsp;Status</a>
            <ul class="collapse list-unstyled" id="StatusSubmenu">
                <li>
                    <a href=@Url.Action("Last_GGG", "Status")><i class="fas fa-industry fa-fw"></i>&nbsp;&nbsp;GGG</a>
                </li>
                <li>
                    <a href=@Url.Action("Last_ASU", "Status")><i class="fas fa-city fa-fw"></i>&nbsp;&nbsp;ASU</a>
                </li>
            </ul>
        </li>
    }

现在,在Status / Last_GGG内部,以下代码可以工作...但是当我取消注释[Authorize(Roles ...)]并注释掉[Authorize]时,我将收到一个身份错误“访问被拒绝-您没有访问权限此资源”。知道为什么会这样吗?

namespace Viewer.Controllers._03_ModuleStatus
{
    [Authorize]
    //[Authorize(Roles = "Module: All - Admin, Module: Status - TechnicianOSC, Module: Status - Admin")]
    public class StatusController : Controller
    {
        private readonly IGGGRepository _GGGRepository;
        public IEnumerable<GGG> ListOfAllGGG;

        public StatusController(IGGGRepository GGGRepository)
        {
            _GGGRepository = GGGRepository;
        }

        public IActionResult Last_GGG()
        {
            GGGPlannerViewModel GGGViewModel = new GGGPlannerViewModel();
            GGGViewModel.GGGList = _GGGRepository.GGGs;
            GGGViewModel.CurrentValues = _GGGRepository.CurrentValuesinDB;
            return View(GGGViewModel);
        }
    }
}

0 个答案:

没有答案