在剃须刀页面中检查登录用户角色 [英] Checking login user role in razor page
本文介绍了在剃须刀页面中检查登录用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@if (Request.IsAuthenticated && User.Identity.Name=="administrator")
{
<div id="sidebar">
<div class="module">
<ul class="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</div>
<div class="mainContent">
Hello, @User.Identity.Name !
</div>
</div>
如果用户通过了管理员身份验证,这是我的布局,但是这种检查效果不好,我需要检查用户的角色而不是他的名字.
This is my layout if the user is authenticated as administrator but this sort of check looks no good, I need to check the role of the user not his name.
这是控制方法
public ActionResult AuthenticatedUserLayout(string username)
{
var lst=userContext.UserProfiles.ToList();
var user = lst.Select(u => u.UserName == username);
if(IsAdmin(Session["LoginUser"].ToString())) return View(user); else return Index();
}
我还发现return View(user)
不好,因为我不知道如何使用该user
.
I also find that return View(user)
is no good, because I don't know how to make any use of that user
.
推荐答案
@if (Request.IsAuthenticated && User.IsInRole("Administrators"))
{
<div id="sidebar">
<div class="module">
<ul class="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</div>
<div class="mainContent">
Hello, @User.Identity.Name !
</div>
</div>
}
这篇关于在剃须刀页面中检查登录用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文