Checking login user role in razor page

前端 未结 3 1966
清酒与你
清酒与你 2020-12-13 05:54
@if (Request.IsAuthenticated && User.Identity.Name==\"administrator\")
{
     
相关标签:
3条回答
  • 2020-12-13 06:11

    For ASP.NET Core Razor Pages

    if (User.Identity.IsAuthenticated && User.IsInRole("Administrator"))
    
    0 讨论(0)
  • 2020-12-13 06:17
    @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>
    }
    
    0 讨论(0)
  • 2020-12-13 06:17

    Dave's answer is correct. I would suggest that you consider using a property on your model called IsAdministrator or CanSeeSidebar and treat answering that question as domain logic.

    The view should work only with the model. Looking at the thread, reading from a database, are the same in respect that they answer domain questions. All those types of questions should be answered before your controller hands the model off to the view.

    0 讨论(0)
提交回复
热议问题