Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 205
Jero Avatar asked Jul 30 '18 20:07

Jero


3 Answers

Note that the order is IMPORTANT. I had these reversed and experienced the exact same problem the original poster experienced. Hours wasted before I figured this out...

    app.UseAuthentication();
    app.UseAuthorization();
like image 143
John81 Avatar answered Oct 11 '22 03:10

John81


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.

like image 36
Kirk Larkin Avatar answered Oct 11 '22 03:10

Kirk Larkin


In addition to @Kirk Larkin answer if you use .net core 3 (in this time is preview version)

put your "app.UseEndpoints" in startup.cs to end of the code block.

for example it should be in this order

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });
like image 4
İsmail Hakkı Şen Avatar answered Oct 11 '22 03:10

İsmail Hakkı Şen