Search code examples
c#asp.net-core-mvc.net-7.0asp.net-authorization

First page in ASP.NET Core 7 index.cshtml


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 
   [email protected]">

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

Solution

  • In your Index.cshtml.cs, try:

    public  IActionResult  OnGet()
    {
        if (!User.Identity.IsAuthenticated)
        {
            return Page();
        }
    
      
        return Redirect("https://localhost:7090/Home/HomePage");
    }