【问题标题】:Hide Admin Links from everyone对所有人隐藏管理员链接
【发布时间】:2011-07-25 15:24:28
【问题描述】:

我正在使用带有剃须刀的 asp.net mvc。如何隐藏仅供管理员使用的链接?

【问题讨论】:

  • 你如何确定某人是否是管理员?

标签: asp.net asp.net-mvc-3 razor


【解决方案1】:

您可以在视图模型上声明一个布尔属性:

public class MyViewModel
{
    public bool IsAdmin { get; set; }

    ... some other model properties
}

在你的视野中:

@if (Model.IsAdmin)
{
    <!-- show the link that only administrators are supposed to see -->
    @Html.ActionLink("Do something very special", "Bar")
}

当然,在渲染此视图的控制器操作中,您将填充此视图模型:

[Authorize]
public ActionResult Foo()
{
    var model = new MyViewModel
    {
        IsAdmin = User.IsInRole("Admin")
    };
    return View(model);
}

显然,只有管理员可以调用的 Bar 操作也应该使用 Authorize 属性进行修饰:

[Authorize(Roles = "Admin")]
public ActionResult Bar()
{
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2011-01-26
    • 2017-09-26
    相关资源
    最近更新 更多