asp.net - 如何根据记录的用户 Asp.net mvc 隐藏/显示菜单项

标签 asp.net asp.net-mvc asp.net-mvc-4

我正在开发一个 ASP.Net MVC 4 应用程序。 _Layout 主视图包含一个菜单,我想根据您是否以用户身份登录来隐藏菜单中的一些项目,并在您以管理员身份登录时显示。

我尝试过的方法确实隐藏了客户端菜单中的链接选项卡,但是当我以管理员身份登录时,当我希望管理员查看它时,它也会隐藏相同的链接选项卡。

只是提到我没有任何角色或管理员 Controller 登录是基于用户的

在此先感谢您的帮助。

<nav>
<ul id="menu">
    <li>@Html.ActionLink("Rep Home", "Index" , "Audit")</li>
    <li>@Html.ActionLink("Log Out", "Login" , "Home")</li>  
    @if (ViewContext.HttpContext.User.IsInRole("Admin"))
    {
        <li><a href="http://example/reports/?report=auditDetails" target="_blank">View  your report</a></li>
    }        
</ul>

public class AccountController : Controller
{
    //
    // GET: /Account/Login

    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    //
    // POST: /Account/Login

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {

        if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, false);
            return RedirectToLocal(returnUrl);
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View(model);
    }





<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
  <forms loginUrl="~/User/Login" timeout="2880" />
</authentication>
<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>

最佳答案

在您的布局页面中试试这个

@if(User.Identity.IsAuthenticated)
{
  <li>Link to show only to logged users</li>
  if(User.IsInRole("Admin"))
  {
    <li>Link show only to Admin </li>
  }
}
else
{
   links that will show to authenticated and unauthenticated users
}

在您的 Controller 中添加这些行

Public ActionResult Login(UserModel model)
{
    // Check user provided credentials with database and if matches write this
       FormsAuthentication.SetAuthCookie(model.id, false);
       return View();
}

最后在您的 Web.config 中将这些行添加到 System.Web 中

<authentication mode="Forms">
  <forms loginUrl="Path of your Login view" timeout="2880"></forms>
</authentication>

请记住,您有 2 个 Web.config 文件,您必须将这些文件添加到较低的 Web.config 文件中。

关于asp.net - 如何根据记录的用户 Asp.net mvc 隐藏/显示菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40931776/

相关文章:

c# - 像 map 一样的谷歌地图

css - 将 margin-top 添加到 html 表格中的图像

c# - 如何在 asp.net 中为不同的浏览器定义不同的 css 文件?

asp.net-mvc - MVC3、 Razor 、Html.TextAreaFor() : adjust height to fit contents

c# - 具有异步操作的异步 Controller 不起作用

asp.net-mvc - 在服务器上安装 ASP.NET MVC 4

asp.net - 如何使用 Eval 更改 TemplateField 中的颜色

javascript - 局部 View 渲染不正确(ASP.NET MVC)

javascript - 从同一页面调用两个按钮不起作用

c# - 在自托管 WebApi 中获取 HttpRequest 上下文