PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 4, 2022

[FIXED] How can I redirect an already logged user to Index in Asp .Net Core web Application (razor pages)

 September 04, 2022     asp.net-core, authentication, c#, razor-pages, web-applications     No comments   

Issue

I want to avoid users getting into the login page if already authenticated.

I know that you can use the RedirectToPage("/index") but i don`t know were.

I've tried this:

@page
@model xxxxx.Accounts.LoginModel
@{
    Layout = "EmptyLayout";
    if (User.Identity.IsAuthenticated)
          RedirectToPage("/index")
}

<!doctype html>
<html lang="es">
//login page

This don't work for me and I know that if (User.Identity.IsAuthenticated) is true, and in the PageModel the Redirect("") method works properly.

Is there any PageModel method like OnCreate(), OnLoad(),... to run code before the page is loaded? How can I achive this?


Solution

I belive that i was not on debug mode so i didn't get the breakpoint on the OnGet(). So the solution is just to do this:

public async Task<IActionResult> OnGet()
        {
            if (User.Identity.IsAuthenticated)
                return Redirect("/Index");
            else return Page();

        }


Answered By - F3rkun
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing