访问显示链接仅 [英] Display links with access only
问题描述
有什么办法如何显示只访问,这是由
Is there any way how to display only links with access, which is defined by
[Authorize(Roles = "SomeRole")]
在控制器?
推荐答案
我不相信有这样做没有反映一个简单的解决方案的控制器
,获得在动作
和检查 AuthorizationFilters
。
I don't believe that there is a simple solution for doing this without reflecting the Controller
, getting the Action
and checking the AuthorizationFilters
.
随着中说,另一种解决方案是创建一个 Html.ActionLink
的扩展方法重载,它接受一个角色名称和检查用户。 IsInRole(角色名)
。只有输出,如果用户的链接访问指定的角色。
With that said, an alternative solution might be to create an extension method overload for Html.ActionLink
which takes a role name and check User.IsInRole(roleName)
. Only output a link if the user has access to the specified role.
事情是这样的:
public static class EntentionMethods
{
public static MvcHtmlString ActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, string roleName)
{
if (!helper.ViewContext.RequestContext.HttpContext.User.IsInRole(roleName))
{
return MvcHtmlString.Empty;
}
return helper.ActionLink(linkText, actionName, controllerName);
}
}
不是pretty你可能一直希望,但是这往往服务的需求。
Not a pretty as you might have been hoping, but this often serves the needs.
这篇关于访问显示链接仅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!