Checking login user role in razor page

asp.net core add role to user
asp.net core 2.2 identity roles
asp.net identity roles example
razor pages role based authorization
asp.net core role claims
razor check if user is logged in
dynamic role based authorization asp.net core
how to check user role in asp net c#
@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.

Here is the controler method

    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();
    }

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>
}

Checking login user AuthorizePolicy in Razor page on Asp.Net Core , but instead of checking after the role I'm after a check into the policy much like you would in a controller by doing this. [Authorize(Policy = Policies. Checking login user role in razor page . I need to check the role of the user not his name. Tags: login Related Posts. Open raw image of a computer (created

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.

View-based authorization in ASP.NET Core MVC, To inject the authorization service into a Razor view, use the @inject directive: the same way you would check during resource-based authorization: Succeeded) { <p><a class="btn btn-default" role="button" href="@Url.Action("Edit​", A user can invoke the Edit action method if he or she knows the  c# - mvc - Checking login user role in razor page role based authentication in asp.net mvc 4 (2)

For ASP.NET Core Razor Pages

if (User.Identity.IsAuthenticated && User.IsInRole("Administrator"))

Role-based authorization in ASP.NET Core, For example, Tracy may belong to the Administrator and User roles whilst Scott may only Role-based authorization checks are declarative—the developer embeds them SetTime() { } [AllowAnonymous] public ActionResult Login() { } }. For Razor Pages, the AuthorizeAttribute can be applied by either:. The specified path is the View Engine path, which is the Razor Pages root relative path. Note on combining authorized and anonymous access. It's valid to specify that a folder of pages that require authorization and than specify that a page within that folder allows anonymous access: // This works. .

How to check if user is in role in a mvc view · Issue #36 , How can I check from a razor view if the active user has a role? With normal built in membership i think you can do the following. @if(User. The user is authenticated via the AuthenticationManager's SignIn method and redirected to the Login page, which will be created next. Create a new Razor Web Page called Login.cshtml and replace the existing code with the following:

Adding Roles to Registration Razor Page in ASP.NET Core, Adding Roles to Registration Razor Page in ASP. We will create two types of users to login through the registration Inside the post handler, when the user is created we are checking if the roles exist in the database or not. There's no built-in way to check if a user is in multiple roles, but it's pretty trivial to create a nice extension method to handle it for you: public static bool IsInAnyRole(this IPrincipal principal, params string[] roles) { return roles.Any(principal.IsInRole); }.

Episode 019 - Roles, claims and policies - ASP.NET , We also took the opportunity of using Razor Pages to see how to implement Then, when creating a user, we could check if the role existed, if not, create the the pattern provided with the current logged in user's username. The solution is to map the user’s Roles to a group of Permissions and store these in the User’s Claims. Then I use ASP.NET Core’s new policy-based authorization system to check that the User’s Permissions Claims contains the Permission placed on the action/page they want to access.

Comments
  • @if (Request.IsAuthenticated && User.IsInRole("administrator"))
  • It's pity that not to support Authorize attribute like [Authorize(Role = "Administrator")] or [Authorize(Policy = "RequireAdministratorRole")]
  • @OzanBAYRAM, you can use Authorize Attribute, it just wouldn't be as convenient. A better way may be to carry around a user object with permission.
  • Thanks, @dave-alperovich, I noticed that I forgot to add page directive to cshtml file. This is why I could not make it work.
  • Is there a way to check this based on Individual Permissions?
  • @JordanLeeBurnes, you can only check if user is an Admin
  • Isn't that hard if you want to put this on a shared view - which with a navigation you almost certainly will?