Openid with keycloak infinite redirect loop after successful login asp net mvc


ASP.NET is a popular language used for building web applications. It provides a framework for developing dynamic websites, web services, and web applications. One common issue that developers may encounter is an infinite redirect loop a successful login when using OpenID with in ASP.NET MVC.

Understanding the Problem

An infinite redirect loop occurs when a user is from one page to another, and then back to the original page in an endless cycle. This can happen due to misconfiguration or incorrect implementation of the authentication process.

Possible Causes

There can be several reasons for an infinite redirect loop after a successful login with OpenID and Keycloak in ASP.NET MVC:

  • Incorrect of the OpenID middleware
  • Missing or incorrect redirect URIs
  • Incorrect handling of authentication callbacks
  • Incorrect implementation of session management


To solve the infinite redirect loop issue, you can follow steps:

Step 1: Check OpenID Middleware Configuration

Ensure that the OpenID middleware is correctly configured in your ASP.NET MVC application. This includes setting the correct client ID, client secret, and authority URL. Make sure to use the configuration values provided by Keycloak.

// OpenID middleware configuration
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
    ClientId = "your-client-id",
    ClientSecret = "your-client-secret",
    Authority = "https://your-keycloak-url/auth/realms/your-realm",
    // other configuration options

Step 2: Verify Redirect URIs

Ensure that the redirect URIs configured in Keycloak match the ones specified in your ASP.NET MVC application. The redirect URI is the URL where the user is redirected after a successful login. If the redirect URIs do not match, it can cause an infinite redirect loop.

Step 3: Handle Authentication Callbacks Correctly

Make sure that you handle the authentication callbacks correctly in your ASP.NET MVC application. After a successful login, the user is redirected back to your application with an authentication token. You need to validate and process this token to complete the authentication process.

// Handle authentication callback
public async Task Callback()
    var result = await .GetOwinContext().Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);
    // process authentication result
    // redirect to the desired page

Step 4: Implement Session Management

Ensure that you have implemented session management correctly in your ASP.NET MVC application. Session management is to maintain the user's authentication state multiple requests. You can use session cookies or other mechanisms provided by ASP.NET to manage user sessions.


An infinite redirect loop after a successful login with OpenID and Keycloak in ASP.NET MVC can be frustrating for both developers and users. By following the steps mentioned above and ensuring correct configuration and implementation, you can resolve this issue and provide a seamless authentication experience for your users.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents