OpenID with keycloak, infinite redirect loop after succesful login ASP.NET MVC 4.7

cebuhax0r 66 Reputation points
2022-12-10T17:10:22.277+00:00

I have setup my ASP.NET MVC 4.7 application like this.
aside from the files bello, nothing has been changed from the original generated project.

The thing is, I can succesfuly redirect to my Keycloak login page, but when it redirects to the url specified after succesful login, it reroutes back to the Identity server (which is keycloak) and the identity server reroutes back to the reroute URL.

here is the dev tools log, it does look like the cookies and sessions are passed properly

After succesful login in Keycloak page it redirects to /home which is correct as that is what i set
269206-image.png

It does looks like cookies are passed properly
![269235-image.png]3
269177-image.png

However, it does seem that after calling /home (redirect) it calls the authentication again in Keycloak
269271-image.png

causing infinite loop.

I already tried the approaches i found in the internet including using UseKentorOwinCookieSaver, using SystemWebCookieManager, and anything i tried online to no luck.

What am I missing here? Help help, im stuck on this issue for days now.

Here is the code

Startup.cs

   using Microsoft.Owin;  
   using Owin;  
   using System;  
   using System.Threading.Tasks;  

   using Microsoft.Owin.Security;  
   using Microsoft.Owin.Security.Cookies;  
   using Owin.Security.Keycloak;  
   using Microsoft.Owin.Security.OpenIdConnect;  
   using Microsoft.IdentityModel.Protocols.OpenIdConnect;  
   using System.IdentityModel.Tokens;  
   using Microsoft.Owin.Host.SystemWeb;  

   [assembly: OwinStartup(typeof(AspNetMVC4.Startup))]  

   namespace AspNetMVC4  
   {  
       public class Startup  
       {  
           public void Configuration(IAppBuilder app)  
           {  
               app.**UseKentorOwinCookieSaver**();  
               // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888  
               const string persistentAuthType = "keycloak_auth";  
               app.SetDefaultSignInAsAuthenticationType(persistentAuthType);  

               app.UseCookieAuthentication(new CookieAuthenticationOptions  
               {  
                   AuthenticationType = persistentAuthType,  
                   AuthenticationMode = AuthenticationMode.Active,  
                   CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebCookieManager()  
               });  

               var desc = new AuthenticationDescription();  
               desc.AuthenticationType = "keycloak_auth";  
               desc.Caption = "keycloak_auth";  

                app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions  
               {  
                   AuthenticationType = "Auth0",  

                   Authority = "http://localhost:8080/auth/realms/master",  

                   ClientId = "keycloakdemo",  
                   ClientSecret = "tUM2gZiW5H3Lx2DQ4b5t4x5FzzrmADGi",  

                   // RedirectUri = "http://localhost:44337/",  
                   //PostLogoutRedirectUri = auth0PostLogoutRedirectUri,  
                   RedirectUri = "https://localhost:44337/home",  

                   ResponseType = OpenIdConnectResponseType.Code,  
                   Scope = "openid profile email",  

                   CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebCookieManager(),  

                      });  

HomeController.cs

   using System;  
   using System.Collections.Generic;  
   using System.Linq;  
   using System.Web;  
   using System.Web.Mvc;  

   namespace AspNetMVC4.Controllers  
   {  
      public class HomeController : Controller  
       {  
           [Authorize]  
           public ActionResult Index()  
           {           
               return View();  
           }  

           public ActionResult About()  
           {  
               bool flag = User.Identity.IsAuthenticated;  
               ViewBag.Message = "Your application description page.";  

               return View();  
           }  

           public ActionResult Contact()  
           {  
               ViewBag.Message = "Your contact page.";  

               return View();  
           }  
       }  
   }  
Microsoft Security | Microsoft Entra | Microsoft Entra ID
Developer technologies | ASP.NET | Other
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.