我想在用户访问时在“布局”视图中隐藏“管理员”标签。 只有具有管理员角色的用户才能看到“管理员”标签。 无论如何要获取查询结果并返回到布局视图,以便视图可以确定用户是否为Admin?
我正在使用Window身份验证模式,它将用户的Windows身份验证作为用户名。无需登录页面。
与此同时,我创建了一个管理页面,将用户名添加为admin。
角色模型:
[Key]
public int Role_ID { get; set; }
public string Permission_Role { get; set; }
用户角色模型:
[Key]
public int Permission_ID { get; set; }
public int Role_ID { get; set; }
public string Role_User { get; set; } <==== will stored @User.Identity.Name
Layout.cshtml:
<div class="menu">
@if (HttpContext.Current.User.Identity.IsAuthenticated)
{
<nav>
<ul id="menu">
<li>
@Html.ActionLink("Home", "SURV_Main_Home", "SURV_Main", null, new { @id = "home" })
</li>
<li>@Html.ActionLink("New Survey", "SURV_Main_Create", "SURV_Main", null, new { @id = "create" })</li>
<li>@Html.ActionLink("Survey List", "SURV_Main_Index", "SURV_Main", null, new { @id = "list" })</li>
if (query get user name is admin then just show this tab) <=======
{
<li>@Html.ActionLink("Admin", "SURV_Admin_Home", "SURV_Admin", null, new { @id = "admin" })</li>
}
</ul>
</nav>
}
</div>
答案 0 :(得分:1)
请执行以下操作:
<div class="menu">
@if (HttpContext.Current.User.Identity.IsAuthenticated)
{
<nav>
<ul id="menu">
<li>@Html.ActionLink("Home", "SURV_Main_Home", "SURV_Main", null, new { @id = "home" })</li>
<li>@Html.ActionLink("New Survey", "SURV_Main_Create", "SURV_Main", null, new { @id = "create" })</li>
<li>@Html.ActionLink("Survey List", "SURV_Main_Index", "SURV_Main", null, new { @id = "list" })</li>
if (User.IsInRole("admin")) <=======
{
<li>@Html.ActionLink("Admin", "SURV_Admin_Home", "SURV_Admin", null, new { @id = "admin" })</li>
}
</ul>
</nav>
}
</div>