'Asp.Net Core5 OpenIdConnect CookieAuthentication SSO not login automatically

I have a .NET Core Api application, and I configured an authentication on an external identity provider.

I can't give the whole code as it's in a separated network, but i configured like this

services.AddScoped<OidcAuthenticationCookieEvents>()
 .AddAuthentication(options =>
 {
     options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
     options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
 })
 .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
 {
     options.Cookie.Name = "mycookieapp";
     options.SlidingExpiration = true;
     options.ExpireTimeSpan = 10;
 })
 .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
 {
     options.RequireHttpsMetadata = false;
     options.Authority = <myidentityprovider>;
     options.ClientId = <myclientId>;
     options.ClientSecret = <myclientsecret>;
     options.Scope.Add(OpenIdConnectScope.OpenId);
     options.Scope.Add(OpenIdConnectScope.OfflineAccess);
     options.TokenValidationParameters = new TokenValidationParameters
     {
        ...
     };
 });

It is working well, when not logged I am redirected to my provider, i log, the provider issues me a SSO cookie, and then the .NET middleware creates an APP cookie.

The problem is, I don't manage to use the SSO cookie. I was expected, if I delete my APP cookie, that I still would be authenticated if i access my API (since the SSO cookie is still valid, the middleware should automatically recreates an APP cookie ?)... But the middleware redirects against login page again...

Is there a specific configuration for having the APP cookie automatically recreated ?

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source