.NET Core Identity Login Page handler OnGetAync()

PixelPaul

I working on a .NET Core 3.1 project using Identity. On the Login.cs.html page, the OnGetAsync() page handler is as follows:

public async Task OnGetAsync(string returnUrl = null)
{
    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;
}

I'm a bit confused about the line of code returnUrl = returnUrl ?? Url.Content("~/"); and why it is written this way. Why is it checking if returnUrl is null, won't it always be null? Why not remove the string returnUrl = null as a method parameter and assign ReturnUrl = Url.Content("~/");? Am I missing some thing here?

Kirk Larkin

The returnUrl parameter has been set up to use an optional argument:

If no argument is sent for that parameter, the default value is used.

With the default setup for Identity, the value for returnUrl comes from the query-string of the request URL. For example, the following URL provides a value for the returnUrl argument:

/Identity/Account/Login?returnUrl=/Some/Protected/Route

In this case, returnUrl will not be null. However, consider the following URL:

/Identity/Account/Login

In this case, returnUrl will be null. There's no URL to redirect to after the login process completes, so the line you've called out will set it to Url.Content("~/"). That just means it'll end up redirecting the user to the root of the web application, which is typically the home page.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.Net Core Identity login status lost after deploy

Register and Login by Phone Number in Asp.net Core Identity

External Login without using identity asp.net core 2.0

use existing login table with asp.net core identity service

.Net Core Identity 2 Provider login Cancel leads to unhandled exception

Asp.net core Identity successful login redirecting back to login page

.net core Identity, getting meaningful login failed reason

ASP.NET Identity remember email address on login page

.NET Core 2 Authenticate with Azure AD - Custom Login Page

Using Asp.Net Core Identity in MVC, Authorize attribute is rebouncing to login page after succesfull login

dotnet core authorize always redirect to identity default login page

Adding Redirection immediately after Login in ASP.Net Core 2.1 using Identity Core

Updating default front-end design of Identity Login Page in .NET core

How to redirect to login page from AuthorizationFilter in asp net core?

Change identity login URL in ASP.net core 3.0

Infinite login redirect loop with Google and ASP.NET Core Identity

.Net Core Identity seed data: unable to login using credentials seeded

ASP.NET Core MVC Identity login issue

.net core identity server 4 authentication handler for oidc

Redirect from .Net Core to Wordpress page with pass login credentials

Print out all usernames in a Razor Page with .net core Identity

link to login does not work, in ASP.NET Core Identity UI

Redirect to login page in asp.net core idenity in ViewComponent

.NET Core: How to send to /Account/Login instead of /Identity/Account/Login

Creating a Login Page in ASP.NET Core MVC

How to set default identity login page in .net core 6 mvc instead of index page in program.cs

Login Page with ASP.NET Core (MVC)

ASP.NET Core Identity Enable Authenticator page

Asp.net Identity keeps redirecting back to login page after successfull login

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive