2

I have created a page which if it is launched for the first time shows login options, etc in the body or index. If we log in then home page is shown but if we close the browser and restart debugger then index is shown again even though user is logged in, where can I change it and how?

I would like to check if the user is logged in then display the home page, but if the user is not logged in, it should display the index page.

HomeController.cshtml:

using Microsoft.AspNetCore.Mvc;
using WebApplication3.Pages.Shared;

namespace WebApplication3.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return View("./Pages/Index");
            }

            return RedirectToAction("./Pages/HomePage");
        }

        public IActionResult HomePage() 
        { 
            return View();
        }
    }
}

Program.cs:

 app.UseHttpsRedirection();
 app.UseDefaultFiles();
 app.UseStaticFiles();
 app.MapControllers();
 app.UseRequestLocalization(locOptions);
 app.UseRouting();
 app.UseAuthentication();
 app.UseAuthorization();
 app.MapControllerRoute(
     name: "default",
     pattern: "{controller=Home}/{action=Index}/{id?}");
 app.MapRazorPages();
 app.Run();

index.cshtml:

@page
@model IndexModel
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

@{
      ViewData["Title"] = "Agencja Nasienna";
      <link rel="stylesheet"              
      href="https://fonts.googleapis.com/css2?family=Plus Jakarta 
      Sans:wght@400;500;700&display=swap" />
      <meta name="viewport" content="initial-scale=2,       
      width=device-width">
}

<style>
    .HomePage {
        width: auto;
        margin-left:4.5vw;
        margin-right:4.5vw;
    }

    .harvesting-wheat-in-tranquil-m {
        position: absolute;
        top: auto;
        left: 0px;
        width: 100%;
        height: 82vh;
        object-fit: cover;
    }

    .platforma-agns-icon {
        position: absolute;
        top: 26.6vh;
        width: 20.3vw;
        height: 16.9vh;
        left: 0px;
        margin-left:4.5vw;    
    }

    .button-wnioski {
        position: relative;
        width: 29.2vw;
        height: 10.4vh;
        top: 43.1vh;
        cursor: pointer;
        text-align: center;
        font-size: 24px;
        color: #0b0b0b;
        font-family: 'Plus Jakarta Sans';
    }

    .button-wnioski-child {
        position: absolute;
        width: 29.2vw;
        height: 10.4vh;
        top: 0;
        right: 0%;
        font-weight: 500;
        left: 0%;
        border:0px;
        border-radius: 10px;
        background-color: #fefefe;
    }

    .button-wnioski1 {
        position: relative;
        width: 29.2vw;
        top: 44.6vh;
        height: 12.1vh;
        cursor: pointer;
        text-align: center;
        font-size: 24px;
        color: #0b0b0b;
        font-family: 'Plus Jakarta Sans';
    }

    .button-wnioski-item {
        position: absolute;
        height: 10.4vh;
        width: 29.2vw;
        top: 0;
        right: 0%;
        left: 0%;
        font-weight: 500;
        border-radius: 10px;
        border: 0px;
        background-color: rgba(255, 199, 166, 0.7);
        letter-spacing: 0.02em;
        display: flex;
        align-items: center;
        justify-content: center;
    }
</style>
<img class="harvesting-wheat-in-tranquil-m" alt=""       
     src="/Image/image_gluten.jpg">

<div class="HomePage">
   <img class="platforma-agns-icon" alt="" src="/Image/PLATFORMA 
   AGN@S.png">

    <a asp-area ="Identity" asp-page="./Account/Login">
        <div class="button-wnioski" id="buttonWnioskiContainer">
            <button class="button-wnioski-child">Zaloguj 
      się</button>
        </div>
    </a>
    <a  asp-area="Identity" asp-page="/Account/Register">
        <div class="button-wnioski1"       
     id="buttonWnioskiContainer1">
            <button class="button-wnioski-item">Uzyskaj            
      dostęp</button>
        </div>
    </a>
</div>
   
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Maksio
  • 23
  • 6
  • " but if we turn off the page then index is shown again even though user is logged in" what do you mean turn off the page ? And what's the result you want ? Could you explain more ? – Qing Guo Aug 30 '23 at 06:46
  • @QingGuo By turning off the page, I meant to restart the project debugger, if we close the browser and restart the debugger, the index will appear – Maksio Aug 30 '23 at 07:01
  • "if we close the browser and restart the debugger, the index will appear " The `/Pages/Index` is the default page , so you will see the `/Pages/Index` first at the restart the debugger. What do you really want ? – Qing Guo Aug 30 '23 at 07:08
  • @QingGuo I would like it to check if the user is logged in and display the home page and if the user is not logged in, it should display the index – Maksio Aug 30 '23 at 07:16
  • "created a page which if it is launched for the first time shows login options" Is your Index page is your login page ? – Qing Guo Aug 30 '23 at 07:18
  • @QingGuo no, contains elements for logging – Maksio Aug 30 '23 at 07:22
  • Try to use `F12` to see the Application tab, is the cookie still retained when you restart the project debugger. – Qing Guo Aug 30 '23 at 07:34
  • `return View("./Pages/Index");` I meet the problem, return view need `viewname` as parameter. – Qing Guo Aug 30 '23 at 08:11
  • @QingGuo I don't quite understand what you mean return view has a path where the index is located – Maksio Aug 30 '23 at 08:17
  • My Index is in the Pages folder. I mean `return View("./Pages/Index");` will meet the error. – Qing Guo Aug 30 '23 at 08:19
  • You have two routes in the program.cs : one is mvc route ,another is razor pages route. As your configured route ,you will excute the Pages Index first, so you can put your `if (!User.Identity.IsAuthenticated)` logic in `OnGet` method of the Razor page Index . – Qing Guo Aug 30 '23 at 08:42
  • @QingGuo And what should I type in if(!User.Identity.IsAuthenticated){} – Maksio Aug 30 '23 at 08:48

1 Answers1

1

In your Index.cshtml.cs, try:

public  IActionResult  OnGet()
{
    if (!User.Identity.IsAuthenticated)
    {
        return Page();
    }

  
    return Redirect("https://localhost:7090/Home/HomePage");
}
Qing Guo
  • 6,041
  • 1
  • 2
  • 10