2016-12-02 5 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なぜ私は新しい質問を投稿したのですか? – cazlouise

+0

このサイトを悪用しないでください - 他の質問を編集して削除してください彼のもの、または他のものを削除する。 –

答えて

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 
} 

レイアウトのページでこれを試してみてください

これらの行を追加します。あなたのWeb.configファイルで
Public ActionResult Login(UserModel model) 
{ 
    // Check user provided credentials with database and if matches write this 
     FormsAuthentication.SetAuthCookie(model.id, false); 
     return View(); 
} 

そして最後に

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

を使用すると、2つのweb.configファイルを持っていて、下のweb.configファイル

内のこれらのファイルを追加する必要が覚えているのsystem.web

内のこれらの行を追加します。
+0

あなたのおかげで、ソリューションをありがとう、私はそれを試みたが、私は理解していない同じを行う – cazlouise

+0

"FormsAuthentication.SetAuthCookie(modal.id、false);"をコントローラに追加し、Formatuthenticationのweb.configを設定しました –

+0

いいえ、私はこのプロセスにどのようにアプローチするのかわかりません。 – cazlouise

関連する問題