显示特定角色的部分视图并隐藏未登录用户的导航按钮
Show parts of view for certain role and hide a nav button from not logged in users
我在 .net 中为学校创建了一个 MVC 项目,并使用此代码仅向具有指定角色的特定用户显示我视图的特定部分。
public ActionResult About()
{
if (User.IsInRole("Begeleider"))
{
var client = new WebClient();
var jsonLeerlingen = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
var leerlingen = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Leerling>>(jsonLeerlingen);
ViewBag.Message = leerlingen;
}
return View();
}
当我使用具有 'Begeleider' 角色的用户登录时,这会起作用,但是当我单击导航中的按钮时,我的 cshtml 中出现错误。这是合乎逻辑的,因为我在这里调用了代码,但是当我没有以正确的角色登录时无法访问它。但是我该如何解决呢?
@{
ViewBag.Title = "Evaluaties";
var leerlingen = List<ASPNetMVCExtendingIdentity2Roles.Domain.Leerling>)ViewBag.Message;
}
<h2>@ViewBag.Title.</h2>
<h4>Leerlingen</h4>
<table>
@foreach (var leerling in leerlingen)
{
<tr>
<td>@leerling.Naam</td>
<td>@leerling.Email</td>
</tr>
}
</table>
<h4>Evaluaties</h4>
@* Here shall be the same code as above but for a Leerling himself he'll only be able to see himself and his own Evaluation(Evaluatie), Haven't figuerd it out yet. *@
导航是这个,最后一个 li 是不应该对未登录用户可见的。
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Roles", "Index", "Roles")</li>
<li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
对操作方法使用授权属性:
//you may use it without role name: [Authorize]
[Authorize(Roles = "Begeleider")]
public ActionResult About()
{
var client = new WebClient();
var jsonLeerlingen = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
var leerlingen = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Leerling>>(jsonLeerlingen);
ViewBag.Message = leerlingen;
return View();
}
如果您想为未使用角色的用户隐藏 link:
if(User.IsInRole("Evaluaties")){
<li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
}
我找到了这样的答案,所以只有登录后才能看到列表项
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
@if (Request.IsAuthenticated)
{
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Roles", "Index", "Roles")</li>
<li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
}
else
{
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Roles", "Index", "Roles")</li>
}
</ul>
@Html.Partial("_LoginPartial")
</div>
我在 .net 中为学校创建了一个 MVC 项目,并使用此代码仅向具有指定角色的特定用户显示我视图的特定部分。
public ActionResult About()
{
if (User.IsInRole("Begeleider"))
{
var client = new WebClient();
var jsonLeerlingen = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
var leerlingen = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Leerling>>(jsonLeerlingen);
ViewBag.Message = leerlingen;
}
return View();
}
当我使用具有 'Begeleider' 角色的用户登录时,这会起作用,但是当我单击导航中的按钮时,我的 cshtml 中出现错误。这是合乎逻辑的,因为我在这里调用了代码,但是当我没有以正确的角色登录时无法访问它。但是我该如何解决呢?
@{
ViewBag.Title = "Evaluaties";
var leerlingen = List<ASPNetMVCExtendingIdentity2Roles.Domain.Leerling>)ViewBag.Message;
}
<h2>@ViewBag.Title.</h2>
<h4>Leerlingen</h4>
<table>
@foreach (var leerling in leerlingen)
{
<tr>
<td>@leerling.Naam</td>
<td>@leerling.Email</td>
</tr>
}
</table>
<h4>Evaluaties</h4>
@* Here shall be the same code as above but for a Leerling himself he'll only be able to see himself and his own Evaluation(Evaluatie), Haven't figuerd it out yet. *@
导航是这个,最后一个 li 是不应该对未登录用户可见的。
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Roles", "Index", "Roles")</li>
<li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
对操作方法使用授权属性:
//you may use it without role name: [Authorize]
[Authorize(Roles = "Begeleider")]
public ActionResult About()
{
var client = new WebClient();
var jsonLeerlingen = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
var leerlingen = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Leerling>>(jsonLeerlingen);
ViewBag.Message = leerlingen;
return View();
}
如果您想为未使用角色的用户隐藏 link:
if(User.IsInRole("Evaluaties")){
<li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
}
我找到了这样的答案,所以只有登录后才能看到列表项
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
@if (Request.IsAuthenticated)
{
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Roles", "Index", "Roles")</li>
<li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
}
else
{
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Roles", "Index", "Roles")</li>
}
</ul>
@Html.Partial("_LoginPartial")
</div>