Asp.net 对所有人隐藏管理员链接

Asp.net 对所有人隐藏管理员链接,asp.net,asp.net-mvc-3,razor,Asp.net,Asp.net Mvc 3,Razor,我正在使用asp.NETMVC和razor。如何隐藏仅用于管理员的链接?您可以在视图模型上声明布尔属性: public class MyViewModel { public bool IsAdmin { get; set; } ... some other model properties } [Authorize] public ActionResult Foo() { var model = new MyViewModel { IsAdmi

我正在使用asp.NETMVC和razor。如何隐藏仅用于管理员的链接?

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

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

    ... some other model properties
}
[Authorize]
public ActionResult Foo()
{
    var model = new MyViewModel
    {
        IsAdmin = User.IsInRole("Admin")
    };
    return View(model);
}
在你看来:

@if (Model.IsAdmin)
{
    <!-- show the link that only administrators are supposed to see -->
    @Html.ActionLink("Do something very special", "Bar")
}
显然,只有管理员才能调用的Bar操作也应该使用
Authorize
属性修饰:

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

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

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

    ... some other model properties
}
[Authorize]
public ActionResult Foo()
{
    var model = new MyViewModel
    {
        IsAdmin = User.IsInRole("Admin")
    };
    return View(model);
}
在你看来:

@if (Model.IsAdmin)
{
    <!-- show the link that only administrators are supposed to see -->
    @Html.ActionLink("Do something very special", "Bar")
}
显然,只有管理员才能调用的Bar操作也应该使用
Authorize
属性修饰:

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

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