2016-12-02 45 views
0

我正在研究ASP.Net MVC 4應用程序。 _Layout主視圖包含一個菜單,我想根據您是否以用戶身份登錄而隱藏菜單中的某些項目,並在您以管理員身份登錄時進行顯示。如何隱藏/顯示基於登錄用戶的菜單項Asp.net mvc

我試過的方法確實隱藏了客戶端菜單中的鏈接選項卡,但是當我以管理員身份登錄時,它也隱藏了相同的鏈接選項卡,當我希望管理員查看它時。

僅舉我沒有任何作用或管理控制器的登錄是基於用戶

將提前得到一些幫助,謝謝。

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

+0

刪除你的[上一個問題](http://stackoverflow.com/questions/40929331/struggling-to-hide-show-menu-item-based-on-logged-users-asp-net-mvc) –

+0

@StephenMuecke我沒有得到答案,我想ereaon爲什麼我發佈了一個新問題 – cazlouise

+0

不要濫用本網站 - 要麼編輯其他問題,並刪除t他的一個,或者刪除另一個。 –

回答

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

+0

感謝您的解決方案,我試過了,但它仍然做同樣的,我不明白 – cazlouise

+0

您是否已在控制器中添加「FormsAuthentication.SetAuthCookie(modal.id,false);」並配置了您的web.config Formatuthentication –

+0

不,我不知道如何處理你提到的這個過程你有什麼樣的例子嗎? – cazlouise

相關問題