Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Tuesday, March 8, 2022

IdentityServer4 .NET Core Ajax Call not working after page is idle for several minutes #Solved

I have faced this problem while trying to implement IdentityServer4 in Microservice .net core project. I tried several codes and spend many days on this.

Finally, it's working fine. I have changed the Cookie settings in both the client-side and Identity server-side application configs.

Please try this and if anyone finds any better solutions, please let me know. Enjoy :)

Client-Side Config

 .AddCookie("Cookies", c =>  
   {  
     c.Cookie.SameSite = SameSiteMode.None;  
     c.Cookie.SecurePolicy = CookieSecurePolicy.Always;  
     c.Cookie.IsEssential = true;  
     c.SlidingExpiration = true;  
     c.ExpireTimeSpan = System.TimeSpan.FromHours(60);  
   }  

In the AddOpenIdConnect set

  options.UseTokenLifetime = false;  

IdentityServer Config

 builder.Services.Configure<CookieAuthenticationOptions>(IdentityServerConstants.DefaultCookieAuthenticationScheme, options =>  
 {  
   options.Cookie.SameSite = SameSiteMode.None;  
   options.Cookie.SecurePolicy = CookieSecurePolicy.Always;  
   options.Cookie.IsEssential = true;  
   options.SlidingExpiration = true;  
   options.ExpireTimeSpan = System.TimeSpan.FromHours(60);  
 });  

Usually, for Cookie-related problems, we don't need to adjust anything on the IdentityServer side.

But it is a little bit complex for me, that's why I have added the settings to both configs.