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

Jero

I have a problem where the asp.net identity framework is redirecting the user back to the login page after they have logged in successfully.

This is using the standard Asp.net Core Identity. It is the version 2.1.1. The scaffolding that generates the razor pages. Not sure if that is significant.

I know the user is successfully logging in because I get the log message

...Areas.Identity.Pages.Account.LoginModel: Information: User logged in.

But then it redirects straight back to the login page.

If I use fiddler I can see that there is a cookie on the request so it all looks good from that perspective.

.AspNetCore.Identity.Application=CfDJ8KJxkuir9ZJIjFLCU2bzm9n6X...

So I guess the middleware that is handling the authentication but not accepting the cookie?

If I could see what the actual middleware for the auth was doing I might have an idea but I can't find it.

Any help appreciated

Kirk Larkin

In order to get the ASP.NET Core pipeline to recognise that a user is signed in, a call to UseAuthentication is required in the Configure method of your Startup class, like so:

app.UseAuthentication();
app.UseMvc(); // Order here is important (explained below).

Using the Cookies authentication scheme, the use of UseAuthentication loosely performs the following:

  • Reads the content of the .AspNetCore.Identity.Application cookie from the request, which represents the identity of the user making the request.
  • Populates the User property of HttpContext with a ClaimsPrincipal that represents said user.

This is a simplified explanation of what happens, but it highlights the important job that the authentication middleware performs. Without the authentication middleware, the .AspNetCore.Identity.Application will not be used for authenticating the user and therefore the user will not be authenticated. In your case, although the user has signed in (i.e. the cookie is being set), the pipeline middleware (e.g. MVC) does not see this user (i.e. the cookie is not being read) and so sees an unauthenticated request and redirects again for login.

Given that the authentication middleware reads the cookie and subsequently populates the ClaimsPrincipal, it should be clear that the UseAuthentication call must also be before the UseMvc call in order for this to occur in the correct order. Otherwise, the MVC middleware runs before the Authentication middleware and will not be working with a populated ClaimsPrincipal.

Why is it failing to login if you don't add the middleware that handles the login?!?

The middleware doesn't handle the login - it handles the authentication process. The user has logged in, which is confirmed by the presence of the .AspNetCore.Identity.Application cookie. What is failing here is the reading of said cookie.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Identity with ASP.Net Core 3.1 - on not authenticated, the app is not redirecting to Login in production like it does in dev

Pass user detail after Login Is successful From identity asp.net core 3.1

User.Identity.IsAuthenticated is false in a non-auth methods after successful login in asp.net core

Flask-login redirecting back to login page

Login Script Redirecting me back to login page

.NET Core Identity Login Page handler OnGetAync()

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

ASP.NET MVC Core 3.0 after login shows login page again at the browser back button click

Redirecting user to default page after login in ASP.NET Core using Okta

Why is my login component not redirecting successful login's to the right page?

Web api not redirecting to identity server login page

Login Page with ASP.NET Core (MVC)

Login Button not redirecting back to home page

Authorized page redirects back to login after successful login

.Net Core 5 Razor pages Cookie Authentication redirects to the login page after successful login

ASP.NET Identity remember email address on login page

ASP.Net Core Identity login status lost after deploy

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

ASP.NET Core MVC Identity login issue

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

External Login without using identity asp.net core 2.0

Change identity login URL in ASP.net core 3.0

use existing login table with asp.net core identity service

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

redirecting to a page after successful login in spring through ajax

Redirecting to another html page after successful login attempt

Header not redirecting upon successful login

OpenCart not redirecting to login page

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