Checking on page _layout.cshtml with Identity framework in asp.net core razor

0

I have the page _layout.cshtml I would like to access all Identity data to display certain part of the menu:

@using Microsoft.AspNetCore.Http
@using CronoParque.Utility
@using Microsoft.AspNetCore.Identity
@inject IHttpContextAccessor HttpContextAccessor
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - CronoParque</title>

    <environment include="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/css/site.css" />
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
        <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
    </environment>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script></head><body><navclass="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a asp-page="/Index" class="navbar-brand">CronoParque</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav nav-pills">
                    @*Cadastros*@
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Cadastros<span class="caret"></span></a>

                        <ul class="dropdown-menu " role="presentation">
                      @if (SignInManager.IsSignedIn(User))
                      {
                            <li><a asp-page="/Cidade/Index">Cidades</a></li>
                      }

In the example above, I check if the user is logged in. But I need to make sure he has permission.

In a normal page where PageModel has already done so.

 ApplicationUser user = await UserManager.GetUserAsync(User);
            bool cidade = user.Cidades;
            if ( cidade == true)
            {
                return Page();
            }
            else
            {
                return RedirectToPage("/Error");
            }

But I have to do within _layout.cshtml that does not have PageModel.

Using @ UserManager.GetUserName (User) I am not able to access all the data that is in the Identity table, maybe because I have instantiate as I did in PageModel, but I do not know how to do this in _layout.cshtml.

Thank you

    
asked by anonymous 18.07.2018 / 16:21

0 answers