3

How to redirect to another page within the _layout.cshtm page in asp.net core razor.

I am doing the verification and user is logged in, if not it will be redirected to another page.

@using Microsoft.AspNetCore.Identity
@using CronoParque.Model
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@if (SignInManager.IsSignedIn(User))
{
    <form asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right">
        <ul class="nav navbar-nav navbar-right">
            <li>
                <a asp-page="/Account/Manage/Index" title="Manage">Ola @UserManager.GetUserName(User)!</a>
            </li>
            <li>
                <button type="submit" class="btn btn-link navbar-btn navbar-link">Sair</button>
            </li>
        </ul>
    </form>
}
else
{
    // targeting here
}
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
Renato
  • 119
  • 1
  • 1
  • 10
  • possible duplicate (https://stackoverflow.com/questions/17653736/redirecting-from-cshtml-page) – Ryan Wilson Jul 18 '18 at 20:04
  • @RyanWilson In asp.net core razor of an error speaking the Response, it does not exist. He had already tried it! Do you know any other way? – Renato Jul 18 '18 at 20:20
  • Why not do it in the controller level ? An action filter would do it. – Shyju Jul 18 '18 at 20:27
  • @Shyju, This is why _Layout.cshtml is the main page and has no controller. – Renato Jul 18 '18 at 20:36
  • @Renato I am just getting used to developing in .Net Core, so I am afraid I can't offer any advice myself. I was hoping that post would help. I know you want to do this from your View, but it is strongly suggested to do the redirect from your Controller. – Ryan Wilson Jul 18 '18 at 20:38
  • @RyanWilson, So when we create a project, it comes to a parent page that is called _Layout.cshtml, this parent page it is displayed on the other child pages ie the parent page has no Controller and neither PageModel precisely because even if I wanted to add a Controller for this page would not have why it is natural of asp.net core razor. – Renato Jul 18 '18 at 20:50
  • But this Layout is executed when rendering a view, which is initiated from an action method. rite ? – Shyju Jul 18 '18 at 20:50
  • @Shyju Sorry, what do you mean? – Renato Jul 18 '18 at 20:58
  • Is this an MVC app/ razor pages ? – Shyju Jul 18 '18 at 20:59

1 Answers1

8

Answer

@if (SignInManager.IsSignedIn(User))
{
    // normal stuff
} 
else if (!Context.Request.Path.ToString().Contains("/About")) 
{
    // If we aren't processing a request for the target page, 
    // then redirect to it.
    Context.Response.Redirect("/About");
}

Maybe a Better Answer

The use case for your question might be to redirect unauthorized requests to a log in page. In that case, use the built-in razor pages authorization API.

services
    .AddMvc()
    .AddRazorPagesOptions(options => {
        options.Conventions.AuthorizePage("/Contact");
    });

The above code goes into the Startup > ConfigureServices method.

In the above example, the API redirects unauthorized requests for the /Contact.

See also: https://learn.microsoft.com/en-us/aspnet/core/security/authorization/razor-pages-authorization?view=aspnetcore-2.1#require-authorization-to-access-a-page

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • Hello, I'm doing the second form you said, but it's always directed to _Layout.cshtml. – Renato Jul 19 '18 at 11:54
  • I put together a sample razor pages app for you https://github.com/shaunluttin/asp-net-core-razor-pages-redirect-unauthorized Does it work for you? – Shaun Luttin Jul 19 '18 at 14:06