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 stand
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();
app.UseAuthentication(); app.UseMvc();
This was already in my code.However I was facing same issue. But the problem was with the Chrome browser on my case.It was working with other browser like mozilla. It starts to work on Chrome too, after i cleared all the cookies and cashes.
I tried many suggestions and finally what worked for me was: First, in your ConfigureServices class within "Startup.cs" add this
services.AddMvc().AddMvcOptions(Mvcoption => {
Mvcoption.EnableEndpointRouting = false;
});
The above code should come after this:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Home/Login";
});
Just in case you don't have the above two codes, try including them in your mvc application. Then you can go ahead and add these in your Configure method.
app.UseAuthentication();
app.UseMvc();
Hope it works now.
Happy coding!
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:
.AspNetCore.Identity.Application cookie from the request, which represents the identity of the user making the request.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.
I had to do a couple of these solutions.
First, I had app.UseAuthentication(); and app.UseAuthorization(); in the incorrect order.
Second, After making the adjustment to startup.cs I was still experiencing the same problem. Doing a hard refresh a few times fixed it (can also just clear the browser cache).
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();
});