在MVC中,如何使用控制器仅为批准的用户渲染局部视图? [英] In MVC how can I use the controller to render a partial view only for approved users?

查看:157
本文介绍了在MVC中,如何使用控制器仅为批准的用户渲染局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC 5中,仅当(经Windows身份验证的)用户属于Active Directory组列表中的一个或多个时,我才尝试使用控制器呈现部分视图.我需要说明30多个不同的组,因此"hello world"示例不符合我的需求.在网上玩寻宝游戏后,我设法收集了这么多钱.没有编译或运行时错误,但内容是针对所有用户而不是特定用户显示的.因此尚未达到预期的结果.

In MVC 5 I am attempting to use the controller to render a partial view only if the (Windows Authenticated) user belongs to one or more of a list of Active Directory groups. There are over 30 distinct groups I need to account for, so the "hello world" examples don't fit my needs. After playing scavenger hunt on the web, I managed to collect this much. No compile or runtime errors, but the content is showing for all users rather than the specific users. So the desired outcome is not yet achieved.

虽然我可以在视图中使用if-then逻辑来达到预期的结果,但它会产生很多不必要的重复并鼓励意大利化.因此,我正在尝试在控制器中执行此操作.

While I can achieve the desired outcome using if-then logic in the view, it creates a lot of unnecessary duplication and encourages spaghettification. So I'm trying to do this in the controller.

期望结果摘要:

当用户加载视图页面时,仅当Windows Authenticated用户属于控制器动作中定义的一组列表中的一个或多个时,才应显示部分视图.如果未授权用户,则不包括部分视图.

When the user loads the viewpage, the partial view should only render if the Windows Authenticated user belongs to one or more of a list of groups defined in the controller action. If the user is not authorized, then the partial view is not included.

控制器块:

[ChildActionOnly]
    [Authorize(Roles="Domain\\GroupA,Domain\\GroupB")]
    public ActionResult MonitorCSU()
    {   
        return PartialView("MonitorCSU");            
    }

查看框:

<div class="rowWithCols3">
@Html.Partial("MonitorCSU")

迭代失败:

  • 在控制器块中,我尝试(未成功)使用if-then块,否则,情况是另一个没有内容的局部视图.

  • In the controller block I tried (unsuccessfully) to use an if-then block, the else case being another partial view with no content.

[仅适用于ChildAction]
公共ActionResult MonitorCSU() { 如果(User.IsInRole("Domain \ GroupA")){ 返回PartialView("_ MonitorCSU"); } 别的 { 返回PartialView("_ Unauthorized"); } }

[ChildActionOnly]
public ActionResult MonitorCSU() { if (User.IsInRole("Domain\GroupA")) { return PartialView("_MonitorCSU"); } else { return PartialView("_Unauthorized"); } }

在Razor中,我尝试使用HTML.Action,但是当我尝试运行页面时,浏览器陷入无限循环.

In Razor, I tried using HTML.Action but when I tried run the page the browser hung in an infinite loop.

推荐答案

@Html.Partial()返回部分视图,而无需调用控制器方法.为了调用您的控制器方法,您需要使用

@Html.Partial() returns a partial view without calling a controller method. In order to call your controller method, you need to use

@Html.Action("MonitorCSU")

@{ Html.RenderAction("MonitorCSU") }

请注意,这假定MonitorCSU()方法与生成主视图的方法位于同一控制器中(否则,您还需要包括一个用于控制器名称的参数)

Note this assumes that the MonitorCSU() method is in the same controller as the method that generates the main view (other wise you also need to include a parameter for the controller name)

参考文档

这篇关于在MVC中,如何使用控制器仅为批准的用户渲染局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆