0
votes

I'm building an application in Asp .NET Core 2.2 using Razor Pages. My Login page is set and I want to be redirected to the main page.

   public IActionResult OnGet()
    {
        try
        {
            if (User.Identity.IsAuthenticated)
                return RedirectToPage("/Account/Index");
        }
        catch (Exception ex)
        {
            Console.Write(ex);
        }

        return Page();
    }



    public async Task<IActionResult> OnPostAsync()
    {
        try
        {
            // Verification.  
            if (ModelState.IsValid)
            {
                // Dominio LDAP
                string domain = "sd6.glb.corp.local";

                // Verificação de autenticação na AD. 
                var isValid = LdapAuthenticate(domain, login.Username, login.Password);

                if (isValid)
                {
                    var user = GetUser();

                    // Login.  
                    await SignInUser(user, login.RememberMe);

                    // Info.  
                    return RedirectToPage("/Account/Index");
                }
                else
                {
                    // Setting.  
                    ModelState.AddModelError(string.Empty, "Username / Password Incorrecto(s).");
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex);
        }

        return Page();
    }

Problem is when I am redirected to Account/Index page

I get this exception

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Abastecimento.Pages.Account.IndexModel', but this ViewDataDictionary instance requires a model item of type 'Abastecimento.Pages.Shared.Pages_Shared__NavbarPartial'.

Here is my layout

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

    <environment include="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/lib/adminlte/dist/css/adminlte.css" />        
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/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"
              crossorigin="anonymous"
              integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"/>
    </environment>
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body class="sidebar-mini">

    <div class="wrapper">
        <!-- Navbar -->
        <partial name="_NavbarPartial" />
        <!-- /.navbar -->
        <partial name="_SidebarPartial" />
        <!-- Main Sidebar Container -->
        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <div class="content-header">
                <div class="container-fluid">
                    @*<div class="row mb-2">
                        <div class="col-sm-6">
                            <h1 class="m-0 text-dark">Starter Page</h1>
                        </div><!-- /.col -->
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item"><a href="#">Home</a></li>
                                <li class="breadcrumb-item active">Starter Page</li>
                            </ol>
                        </div><!-- /.col -->
                    </div><!-- /.row -->*@
                </div><!-- /.container-fluid -->
            </div>
            <!-- /.content-header -->
            <!-- Main content -->
            <div class="content fade-in">
                <div class="container-fluid my-5 pb-4">

                    @RenderBody()

                </div><!-- /.container-fluid -->
            </div>
            <!-- /.content -->
        </div>
        <!-- /.content-wrapper -->
        <!-- Main Footer -->
        <partial name="_FooterPartial" />   
    </div>


    <environment include="Development">
        <script src="~/lib/jquery/dist/jquery.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
        <script src="~/lib/adminlte/dist/js/adminlte.js"></script>
    </environment>
    <environment exclude="Development">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery"
                crossorigin="anonymous"
                integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=">
        </script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"
                asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
                crossorigin="anonymous"
                integrity="sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o">
        </script>
    </environment>

    <script src="~/js/site.js" asp-append-version="true"></script>

    @RenderSection("Scripts", required: false)
</body>
</html>

and this is my navbar partial

@page

<!-- Navbar -->
<nav class="main-header navbar navbar-expand navbar-dark bg-danger border-bottom custom-sidebar fixed-top">
    <!-- Left navbar links -->
    <ul class="navbar-nav">
        <li class="nav-item ">
            <a class="nav-link custom-nav-link custom-navbar-links" data-widget="pushmenu" href="#"><i class="fas fa-bars"></i></a>
        </li>
        <li class="nav-item d-none d-sm-inline-block">

            <a href="/Account/Index" class="nav-link custom-navbar-links">
                <p>
                    <i class="fas fa-home"></i>
                    Início
                </p>
            </a>

        </li>
        <li class="nav-item d-none d-sm-inline-block">

            <a href="/Account/Home/Suporte" class="nav-link custom-navbar-links">
                <p>
                    <i class="fas fa-question"></i>
                    Suporte
                </p>
            </a>

        </li>
    </ul>

    <!-- Right navbar links -->
    <ul class="navbar-nav ml-auto">

        <li class="nav-item">
            <a class="nav-link custom-navbar-links" href="/Account/Logout">
                <i class="fas fa-sign-out-alt"></i>
                Terminar Sessão
            </a>
        </li>
    </ul>
</nav>
<!-- /.navbar -->

this works correctly on asp net MVC 5 using partial views, but i can't figure out what's causing this error.

1

1 Answers

0
votes

Usually you should redirect from login page to home/index. If you send Index page I can tell you better reason.