根据登录用户Asp Net Core 2

时间:2018-03-23 13:20:14

标签: asp.net-mvc asp.net-core

我试图找出使用身份验证来控制显示哪个ul标记的最佳方法

Curent html

 //wrap all this is authentication razor code?
 <!-- START Profile button menu -logged in -->
 <ul class="dropdown-menu dropdown-menu-right">
     <li class="dropdown-header">Profile</li>
     <li>
         <a tabindex="-1" asp-area="" asp-controller="Manage" asp-action="Index">
             <i class="si si-envelope-open pull-right"></i>
             <span class="badge badge-primary pull-right">3</span>Inbox
         </a>
     </li>
     <li>
         <a tabindex="-1" asp-area="" asp-controller="Manage" asp-action="Index">
             <i class="si si-user pull-right"></i>
             <span class="badge badge-success pull-right">1</span>Profile
         </a>
     </li>
     <li>
         <a tabindex="-1" href="javascript:void(0)">
             <i class="si si-settings pull-right"></i>Settings
         </a>
     </li>
     <li class="divider"></li>
     <li class="dropdown-header">Actions</li>
     <li>
          <a tabindex="-1" asp-area="" asp-controller="Account" asp-action="Lockout">
              <i class="si si-lock pull-right"></i>Lock Account
          </a>
     </li>
     <li>
          <a tabindex="-1" asp-area="" asp-controller="Account" asp-action="Logout">
              <i class="si si-logout pull-right"></i>Log out
          </a>
     </li>
</ul>
<!-- END Profile button menu -logged in-->
<!-- START Profile button menu - not logged in-->
<ul class="dropdown-menu dropdown-menu-right">
     <li class="dropdown-header">Actions</li>
     <li>
         <a tabindex="-1" asp-area="" asp-controller="Account" asp-action="Login">
             <i class="si si-logout pull-right"></i>Login
         </a>
     </li>
     <li>
         <a tabindex="-1" asp-area="" asp-controller="Register" asp-action="Register">
            <i class="si si-lock pull-right"></i>Register
         </a>
     </li>
</ul>
<!-- END Profile button menu - not logged in -->

我在Dave Paquette上发现了一篇关于自定义标签助手的博客文章,但是我无法让它发挥作用。这个html是我对标题导航的局部视图所以我不能(或者不知道如何在控制器中检查这个)所以我选择使用Razor测试登录用户。

我在简单的登录用户检查中看不到任何博客或故事。它们都与政策,索赔等有关。我只是想知道用户是否登录并显示依赖于此的ul。

1 个答案:

答案 0 :(得分:0)

我认为您需要使用SignInManager来查看是否有用户登录。这将使用Claims。如果您使用的是AplicationUser模型,则可以执行以下操作。

在您的剃须刀页面上使用:

inject SignInManager<ApplicationUser> SignInManager

然后:

@if (SignInManager.IsSignedIn(User))
{
    //some html...
}