【发布时间】: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>
【问题讨论】:
-
@StephenMuecke 我没有得到我想要的答案,因为我发布了一个新问题
-
不要滥用这个网站 - 要么编辑另一个问题并删除这个问题,要么删除另一个。
标签: asp.net asp.net-mvc asp.net-mvc-4