![](/img/trans.png)
[英]MVC webapp with Azure AD authentication works fine in local but not when porting to Azure app services
[英]MVC Webapp hide/show website tabs with Azure AD Roles
我有一個MVC WebApp,可以在其中成功進行身份驗證並將Azure AD角色提取到我的應用程序中。 如何根據該角色在_Layout.cshtml文件中顯示或隱藏導航欄選項卡,例如“ home”,“ about”等?
我可以使用[Authorize(Roles = "")]
在控制器中授權頁面,但是我想在導航欄級別隱藏。 我缺少哪一行代碼來進行這種動態測試?
這是我希望實現此目標的_Layout.cshtml文件中的代碼:
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("My Project", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
先謝謝您的幫助!
在MVC上,您具有User
對象,該對象為您提供有關已記錄用戶的信息。 只需使用User.IsInRole(roleName)
方法,它應該可以工作。
例:
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("My Project", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
@if(User.IsInRole("Admin"))
{
//Only the user with "Admin" role can see this
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
}
</ul>
@Html.Partial("_LoginPartial")
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.