Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net MVC3:实际上基于角色@Html.Action()_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 3_Razor - Fatal编程技术网

C# Asp.net MVC3:实际上基于角色@Html.Action()

C# Asp.net MVC3:实际上基于角色@Html.Action(),c#,asp.net,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Razor,我的视图使用@Html.Action(…)来反映各种功能块。在打开页面时,站点将为在控制器方法中没有角色指向的用户显示授权对话框。(如“经理”或“来电者”)按“取消”获取: 401-未经授权:由于凭据无效,访问被拒绝。 如果用户没有必需的角色,@Html.Action被忽略或没有显示任何内容,我是否可以更改我的应用程序行为 我的看法是: @model InApp.ViewModel.ListViewModel @{ Layout = "~/Views/Shared/_Layout.csh

我的视图使用
@Html.Action(…)
来反映各种功能块。在打开页面时,站点将为在控制器方法中没有角色指向的用户显示授权对话框。(如“经理”或“来电者”)按“取消”获取: 401-未经授权:由于凭据无效,访问被拒绝。 如果用户没有必需的角色,@Html.Action被忽略或没有显示任何内容,我是否可以更改我的应用程序行为

我的看法是:

@model InApp.ViewModel.ListViewModel
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="CallList">
@Html.Action("List","Call",new {id=Model.id})
</div>

<div class="Order">
@Html.Action("Create","Order",new {id=Model.id})
</div>
为了找到正确的解决方案,我发现了类似的问题:


但在我看来,我不喜欢“如果”条件。我正在寻找一个复杂的解决方案,只使用AuthorizeAttribute隐藏和显示单独的部分,并避免在视图或控制器中使用if–else。谢谢你的帮助

如果您不喜欢视图中的此逻辑,请将其移到其他位置。例如,您可以创建自己的扩展方法并像
@Html.ActionOrNothing(…)
那样使用它


实现应该检查用户是否有权查看某些内容,否则返回空字符串/视图。

如果您不喜欢视图中的此逻辑,请将其移动到其他位置。例如,您可以创建自己的扩展方法并像
@Html.ActionOrNothing(…)
那样使用它


实现应该检查用户是否有查看内容的权限,否则返回空字符串/视图。

我建议使用此扩展方法:

这是@Html.Action的包装,它使用反射检查用户权限

public static  MvcHtmlString ActionBaseRole(this HtmlHelper value, string actionName, string controllerName, object routeValues , IPrincipal user)
 {     
   bool userHasRequeredRole = false;
   Type t = Type.GetType((string.Format("MyProject.Controllers.{0}Controller",controllerName))); // MyProject.Controllers... replace on you namespace
   MethodInfo method = t.GetMethod(actionName);
   var attr = (method.GetCustomAttribute(typeof(AuthorizeAttribute), true) as AuthorizeAttribute);
   if (attr != null)
   {
      string[] methodRequeredRoles = attr.Roles.Split(',');
      userHasRequeredRole = methodRequeredRoles.Any(r => user.IsInRole(r.Trim())); // user roles check in depends on implementation authorization in you site  
                                                                                            // In a simple version that might look like                                                                         
   }
   else userHasRequeredRole = true; //method don't have Authorize Attribute
   return userHasRequeredRole ? value.Action(actionName, controllerName, routeValues) : MvcHtmlString.Empty; 
 }
在视图中使用:

@Html.ActionBaseRole("List","Call",new {id=Model.id},User)

我可以建议使用这种扩展方法:

这是@Html.Action的包装,它使用反射检查用户权限

public static  MvcHtmlString ActionBaseRole(this HtmlHelper value, string actionName, string controllerName, object routeValues , IPrincipal user)
 {     
   bool userHasRequeredRole = false;
   Type t = Type.GetType((string.Format("MyProject.Controllers.{0}Controller",controllerName))); // MyProject.Controllers... replace on you namespace
   MethodInfo method = t.GetMethod(actionName);
   var attr = (method.GetCustomAttribute(typeof(AuthorizeAttribute), true) as AuthorizeAttribute);
   if (attr != null)
   {
      string[] methodRequeredRoles = attr.Roles.Split(',');
      userHasRequeredRole = methodRequeredRoles.Any(r => user.IsInRole(r.Trim())); // user roles check in depends on implementation authorization in you site  
                                                                                            // In a simple version that might look like                                                                         
   }
   else userHasRequeredRole = true; //method don't have Authorize Attribute
   return userHasRequeredRole ? value.Action(actionName, controllerName, routeValues) : MvcHtmlString.Empty; 
 }
在视图中使用:

@Html.ActionBaseRole("List","Call",new {id=Model.id},User)

如果您试图完全避免if语句,则可以将这些链接(@html.actions在视图上)移动到各自的局部视图。如果您试图完全避免if语句,则可以将这些链接(@html.actions在视图上)移动到各自的局部视图。