简体   繁体   中英

ASP.NET Core log-in not redirecting me to home page

I am creating a login and registration with identity in asp.net core c#, I configured all the files and the database, and double checked and saw that the user is in AspNetUsers so the connection between the database and the program is working. I put when user is authenticated, it redirects to the home page but it is staying on the Log-In Page.

 public async Task OnGetAsync(string returnUrl = null)
        {
            if (User.Identity.IsAuthenticated) { Response.Redirect("Home"); }
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                ModelState.AddModelError(string.Empty, ErrorMessage);
            }

            returnUrl = returnUrl ?? Url.Content("~/");

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            ReturnUrl = returnUrl;
        }

Try to modify the code as below.

if (User.Identity.IsAuthenticated) {
    Response.Redirect("/Home/Index");
}

Test Result

在此处输入图像描述

Replace below code:

Response.Redirect("Home");

New Code:

return new RedirectToPageResult("/Home/Index");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM