如何显示或隐藏基于角色的控制 - ASP.NET MVC 4剃刀 [英] How to Show or hide controls based on roles - ASP.NET MVC 4 Razor

查看:646
本文介绍了如何显示或隐藏基于角色的控制 - ASP.NET MVC 4剃刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米工作的ASP.NET MVC 4 application.I有一个仪表板和我的用户群体将基于Windows域 所以我使用Windows身份验证来验证用户。 我创建示例应用程序它通过覆盖功能AuthorizeAttribute,ActionFilterAttribute使用自定义验证。这是一个很好的方法?

  
      
  1. 在哪个属性最适合用于身份验证?
  2.   

我有一个仪表盘。所以,我需要显示或隐藏基于角色的控制。  假设,如果有3格(表),如果管理员在,他可以能看到3个网格(表)日志。 但是,如果支持用户登录,他可以看到2个网格(表)只。

我的计划是为每个网格部分意见,这样就会有一个动作和控制器的每个部分的视图。 将会有一个数据库,并在将要指定的,其中每个基团可以执行的操作。所以,我可以过滤请求。

  

2我怎样可以隐藏或显示基于角色的部分看法?

我尝试了一些SO联系,但他们所谈论的是2,​​3的角色,这是很难codeD。 在我的情况的作用可能会有所不同,我们使用DB来设置访问权限的角色。

在此先感谢。

解决方案

我做了类似的事情。我做的方式(可能不是最好的)

是发送一个布尔值回查看

在控制器中使用:

 布尔showSection1 = TRUE;
布尔showSection2 = FALSE;

计算机[showSection1] = showSection1;
计算机[showSection2] = showSection2;
///可能会更好使用视图模型在这里,而不是ViewData的
 

然后在视图

@if((布尔)计算机[showSection1])    {      @ {Html.RenderPartial(section1第);}    }    @if((布尔)计算机[showSection2))    {       @ {Html.RenderPartial(第2节);}    }

您需要做的逻辑来设置布尔你想要的方式,但是这应该是一个开始。

你也可以创建一个返回的角色,然后让该值直接从该视图的静态方法。这可能是坏的形式,虽然。

  @if(AppHelper.GetRole()。等于(基于role1)){
    //显示部分
}
 

然后创建一个类叫做AppHelper和一个名为GetRole方法,它返回用户的角色。

I m working on ASP.NET MVC 4 application.I have a dashboard and my users groups will be based on Windows Domain So I am using WIndows Authentication for authenticating users. I created sample applications which uses custom authentication by overrides functions AuthorizeAttribute, ActionFilterAttribute . Is this a good approach ?

  1. Which attribute is best used for authentication ?

I have a dashboard. So I need to show or hide the controls based on roles. Suppose if there is 3 grids(table), If Admin is logs in, he can able see 3 grids(tables). But if Support user is log in he can see 2 grids (table) only.

My plan is to create partial views for each grid and so there will be an Action and Controller for each partial view. There will be a database and in that I will specify the actions which each group can perform. So that I can filter the requests.

2 How can I hide or show the partial views based on roles ?.

I tried some SO links, but all they are talking about 2,3 roles and it was hard coded. In my case roles may vary and we uses db to set up access for roles.

Thanks in advance.

解决方案

I have done something similar. The way I did it (may not be the best)

is to send a boolean back to the view

in the controller use:

bool showSection1 = true;
bool showSection2 = false;

ViewData["showSection1"] = showSection1;
ViewData["showSection2"] = showSection2;
/// may be better to use a viewmodel here rather than ViewData

then in the view

@if((bool)ViewData["showSection1"]) { @{Html.RenderPartial("section1");} } @if((bool)ViewData["showSection2")) { @{Html.RenderPartial("Section2");} }

you will need to do the logic to set the boolean the way you want them but this should be a start.

you could also create a static method that returns the role and then get that value directly from the view. this may be bad form though.

@if(AppHelper.GetRole().equals("role1")){
    //show the partial
}

then create a class called AppHelper and a method called GetRole that returns the role of the user.

这篇关于如何显示或隐藏基于角色的控制 - ASP.NET MVC 4剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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