【问题标题】:How Can I hide/show menu item based on Logged users Asp.net mvc如何根据记录的用户 Asp.net mvc 隐藏/显示菜单项
【发布时间】:2017-04-17 08:07:45
【问题描述】:

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

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

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

在此先感谢您的帮助。

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

【问题讨论】:

  • 删除你的previous question
  • @StephenMuecke 我没有得到我想要的答案,因为我发布了一个新问题
  • 不要滥用这个网站 - 要么编辑另一个问题并删除这个问题,要么删除另一个。

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


【解决方案1】:

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

@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
}

在您的控制器中添加这些行

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 文件中。

【讨论】:

  • 谢谢你的解决方案,我试过了,但还是一样,我不明白
  • 您是否在控制器中添加了“FormsAuthentication.SetAuthCookie(modal.id,false);”并配置了您的 web.config 以进行 Formatuthentication
  • 不,我不知道如何处理你提到的这个过程,请问你有什么例子吗?
  • 在您的控制器操作中添加以下代码 FormsAuthentication.SetAuthCookie(model.id,false);
  • 在索引操作中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 2019-01-27
相关资源
最近更新 更多