访问显示链接仅 [英] Display links with access only

查看:105
本文介绍了访问显示链接仅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法如何显示只访问,这是由

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屋!

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