How to resolve infinite loop issue in Azure AD authentication in Asp.Net MVC application

Arpit Tandon 0 Reputation points
2025-09-24T10:31:22.97+00:00

We have an Asp.Net MVC application running on v4.6.1. We are implementing SSO authentication using Azure AD. We are using Microsoft.Owin.Security.OpenIdConnect library for cookie based authentication.

Below is our auth configuration -

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {

        AuthenticationType = "Cookies",
        CookieSecure = CookieSecureOption.Always,
        CookieSameSite = Microsoft.Owin.SameSiteMode.None,
        CookieManager = new SystemWebChunkingCookieManager()
    });

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = ClientId,
            Authority = Authority,
            ClientSecret = OidcClientSecret,
            PostLogoutRedirectUri = PostLogoutRedirectUri,
            RedirectUri = PostLogoutRedirectUri,
            Scope = OpenIdConnectScope.OpenIdProfile + " roles",
            ResponseType = OpenIdConnectResponseType.Code,                    
            TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = false // Set to true in production if you know the exact issuer
            }
        });
}

Problem -

We are facing infinite loop issue during authentication where /authorize endpoint call and localhost redirect calls are going in loop. Eventually, with multiple nonce cookies being sent, the request becomes too large and fails with 400 Bad Request or we get the error saying "we couldn't sign you in".

Solutions we have tried -

  • Using https for local development so that nonce cookie with secure attribute can be set

Using SystemWebChunkingCookieManager() as CookieManager

We are still stuck with the problem and need suggestions for resolution.

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Raymond Huynh (WICLOUD CORPORATION) 2,215 Reputation points Microsoft External Staff
    2025-09-25T09:11:14.95+00:00

    Hello Arpit Tandon, this redirect loop with Azure AD + OWIN is typically caused by a callback/redirect mismatch, cookie settings, or an auth type mismatch. Here are my recommendations:

    • Make the redirect exact
      • Set RedirectUri = https://<host>:<port>/signin-oidc and CallbackPath = "/signin-oidc".
      • Register that exact URI (scheme/host/port/path) in Entra ID; don’t reuse PostLogoutRedirectUri.
    • Order and auth types
      • Call app.SetDefaultSignInAsAuthenticationType("Cookies").
      • Use UseCookieAuthentication(...) before UseOpenIdConnectAuthentication(...).
      • In OIDC: SignInAsAuthenticationType = "Cookies" (must match cookie AuthenticationType).
    • Cookies/SameSite/nonce
      • CookieSecure = Always, CookieSameSite = None, CookieManager = new SystemWebChunkingCookieManager().
      • Prevents nonce/correlation cookie loops and large headers.
    • HTTPS only
      • Run on HTTPS even locally; mixed HTTP/HTTPS drops secure cookies and retriggers auth.
    • Update OWIN/Katana (optional helper)

    Minimal working config:

    app.SetDefaultSignInAsAuthenticationType("Cookies");
     
    app.UseCookieAuthentication(new CookieAuthenticationOptions {
        AuthenticationType = "Cookies",
        CookieSecure = CookieSecureOption.Always,
        CookieSameSite = Microsoft.Owin.SameSiteMode.None,
        CookieManager = new SystemWebChunkingCookieManager()
    });
     
    // optional (legacy): app.UseKentorOwinCookieSaver();
     
    app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions {
        ClientId = ClientId,
        Authority = Authority,
        ClientSecret = OidcClientSecret,
        RedirectUri = "https://localhost:44300/signin-oidc",
        PostLogoutRedirectUri = "https://localhost:44300/",
        CallbackPath = new PathString("/signin-oidc"),
        ResponseType = OpenIdConnectResponseType.Code,
        SignInAsAuthenticationType = "Cookies",
        TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false } // set true in production
    });
    

    If a loop persists, confirm X-Forwarded-Proto=https behind proxies and use a shared machine key across instances. Hope this solves your problem.


Your answer

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