从某些角色在ASP.NET MVC5隐藏链接 [英] Hiding links from certain roles in ASP.NET MVC5

查看:250
本文介绍了从某些角色在ASP.NET MVC5隐藏链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这听起来像一个愚蠢的问题,但我怎么隐藏比如说用户的链接,但表明了它在管理主要布局网页?

So this may sound like a dumb question, but how do I hide a link from say a user but show it for admin in the main layout page?

当一个普通用户logsin或匿名用户查看他们看到的网页说:结果
首页/关于/联系结果
而当管理员登录时,他们看到:结果
首页/关于/联系我们/管理员

Say when a regular user logsin or an anonymous user is viewing the page they see:
Home / About / Contact
And when an Admin logs in, they see:
Home / About / Contact / Admin

我试图限制在控制器和菜单上的控制器连接。但它仍然显示的链接给大家,只是不允许访问任何人,但系统管理员

I tried restricting in the controller and linking the controller on the menu. But it still shows the link for everyone, just doesn't allow access to anyone but admin

可以在视图超载?

推荐答案

根据什么样您使用会员/用户提供的,你应该能够直接从View来检查,如果用户在和登录具体作用。

Depending on what sort of Membership/User provider you are using, you should just be able to check directly from the View if the user is logged in and in the specific role.

所以,你最终会喜欢的东西;

So you would end up with something like;

@Html.ActionLink("Index", "Home") 
@Html.ActionLink("About", "Home") 
@Html.ActionLink("Contact", "Home") 
@if ( User.Identity.IsAuthenticated ){
    if ( User.IsInRole("Admin") ){
        @Html.ActionLink("Admin", "AdminController")        
    }
}

和记得 [授权] 属性添加到您的管理​​操作方法:

And remember to add [Authorize] attribute to your Admin action method:

[Authorize(Roles="Admin")]
public ActionResult Admin()
{
    // ...
    return View();
}

这篇关于从某些角色在ASP.NET MVC5隐藏链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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