IsAuthenticated always false after successful login

I have a .NET Core application which successfully redirect to the okta login page and after entering the correct credentials it goes back to the Login action with the IsAuthenticated property always false.

Any idea of why this is happening?

Trusted origin: https://localhost:5001/
Login URL: https://localhost:5001/admin/account/login

        public IActionResult Login()
        {
            if (!HttpContext.User.Identity.IsAuthenticated)
            {
                return Challenge(OpenIdConnectDefaults.AuthenticationScheme);
            }
            return RedirectToAction("Index", "Home");
        }
services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options =>
            {
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.Authority = Configuration["Okta:Domain"] + "/oauth2/default";
                options.RequireHttpsMetadata = true;
                options.ClientId = Configuration["Okta:ClientId"];
                options.ClientSecret = Configuration["Okta:ClientSecret"];
                options.ResponseType = OpenIdConnectResponseType.Code;
                options.GetClaimsFromUserInfoEndpoint = true;
                options.Scope.Add("openid");
                options.Scope.Add("profile");
                options.SaveTokens = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "name",
                    RoleClaimType = "groups",
                    ValidateIssuer = true
                };
            });
            services.AddAuthorization();
app.UseAuthentication();
            app.UseAuthorization();

I just found my own mistake. The problem was that app.UseAuthentication() must be added before app.UseMvc().

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.