Skip to content

Commit a042abe

Browse files
author
Simonx Xu
authored
Merge pull request #8698 from AmandaAZ/Branch-CI4678
AB#4678: Convert blog post to article
2 parents f25f16f + 6a78485 commit a042abe

3 files changed

Lines changed: 147 additions & 1 deletion

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
title: Infinite Sign-in Loop Between ASP.NET Application and Microsoft Entra ID
3+
description: Helps you resolve an infinite sign-in loop issue between an ASP.NET application and with Microsoft Entra ID when performing sign in.
4+
ms.date: 04/25/2025
5+
ms.reviewer: bachoang, v-weizhu
6+
ms.service: entra-id
7+
ms.custom: sap:Developing or Registering apps with Microsoft identity platform
8+
---
9+
10+
# Infinite sign-in loop between ASP.NET application and Microsoft Entra ID
11+
12+
This article provides solutions to an issue where an ASP.NET application experiences an infinite redirect loop during signing in with Microsoft Entra ID.
13+
14+
## Symptoms
15+
16+
An ASP.NET application running an earlier version of Open Web Interface for .NET (OWIN) middleware fails to recognize an authenticated request from Microsoft Entra ID. It keeps sending the request back to Microsoft Entra ID for signing in, leading to the infinite loop issue. The following error message might be displayed in the browser:
17+
18+
> We couldn't sign you in. Please try again.
19+
20+
## Cause
21+
22+
This issue occurs due to a cookie mismanagement issue (a [known Katana bug](https://github.com/aspnet/AspNetKatana/wiki/System.Web-response-cookie-integration-issues)) in the earlier version of OWIN.
23+
24+
### How to recognize the Katana bug
25+
26+
Capture a Fiddler trace and examine one of the later redirect frames back to the web application. Note in the following screenshot, the request in frame 58 contains multiple OpenIdConnect.nonce cookies (red-circled). In a working scenario, you should only have one OpenIdConnect.nonce cookie set at the beginning before authentication. After the request is successfully authenticated, this nonce cookie is destroyed and ASP.NET sets its own session cookie. Because of this bug, you see a buildup of these nonce cookies.
27+
28+
:::image type="content" source="media/asp-dot-net-application-infinite-sign-in-loop/openidconnet-nonce-cookies.png" alt-text="Screenshot that shows multiple OpenIdConnect nonce cookies." lightbox="media/asp-dot-net-application-infinite-sign-in-loop/openidconnet-nonce-cookies.png":::
29+
30+
## Solution 1: Upgrade to ASP.NET Core
31+
32+
The issue is resolved in ASP.NET Core and a later version of Katana OWIN for ASP.NET. To resolve this issue, upgrade your application to use ASP.NET Core.
33+
34+
If you must continue to use ASP.NET, perform the following actions:
35+
36+
- Update your application's Microsoft.Owin.Host.SystemWeb package to version 3.1.0.0 or later.
37+
- Modify your code to use one of the new cookie manager classes, for example:
38+
39+
```csharp
40+
app.UseCookieAuthentication(new CookieAuthenticationOptions
41+
{
42+
AuthenticationType = "Cookies",
43+
CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager()
44+
});
45+
```
46+
or
47+
48+
```csharp
49+
app.UseCookieAuthentication(new CookieAuthenticationOptions()
50+
{
51+
CookieManager = new SystemWebCookieManager()
52+
});
53+
```
54+
55+
## Solution 2: Correct the redirect URL
56+
57+
In some cases where the application is hosted under a virtual directory or an application instead of the root of the web site, [solution 1](#solution-1-upgrade-to-aspnet-core) might not work. For more information, see [Infinite re-direct loop after AAD Authentication when redirect is specified](https://stackoverflow.com/questions/44397715/infinite-re-direct-loop-after-aad-authentication-when-redirect-is-specified) and [Microsoft Account OAuth2 sign-on fails when redirect URL is not under the website root](https://github.com/aspnet/AspNetKatana/issues/203).
58+
59+
For example, suppose you have the following environment:
60+
61+
- The root of a web site: `https://mysite` – This site runs under *Application Pool 1*.
62+
- An application *test2* under the root: `https://mysite/test2` – This application runs under *Application Pool 2*.
63+
- Your ASP.NET application runs under the *test2* application with the following code:
64+
65+
```csharp
66+
public void Configuration(IAppBuilder app)
67+
{
68+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
69+
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
70+
app.UseCookieAuthentication(new CookieAuthenticationOptions());
71+
app.UseOpenIdConnectAuthentication(
72+
new OpenIdConnectAuthenticationOptions
73+
{
74+
// Sets the ClientId, authority, RedirectUri as obtained from web.config
75+
ClientId = clientId,
76+
Authority = authority,
77+
RedirectUri = "https://mysite/test2",
78+
// PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
79+
PostLogoutRedirectUri = redirectUri,
80+
Scope = OpenIdConnectScope.OpenIdProfile,
81+
// ResponseType is set to request the id_token - which contains basic information about the signed-in user
82+
ResponseType = OpenIdConnectResponseType.IdToken,
83+
// ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
84+
// To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name
85+
// To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter
86+
87+
// OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
88+
89+
Notifications = new OpenIdConnectAuthenticationNotifications
90+
{
91+
AuthenticationFailed = OnAuthenticationFailed
92+
}
93+
94+
}
95+
);
96+
}
97+
```
98+
99+
- You use the following code to trigger the sign-in flow:
100+
101+
```csharp
102+
public void SignIn()
103+
{
104+
if (!Request.IsAuthenticated)
105+
{
106+
HttpContext.GetOwinContext().Authentication.Challenge(
107+
new AuthenticationProperties { RedirectUri = "/" },
108+
OpenIdConnectAuthenticationDefaults.AuthenticationType);
109+
}
110+
}
111+
```
112+
113+
This scenario can result in an authentication infinite loop with a buildup of multiple OpenIdConnect.nonce cookies. The difference is that ASP.NET doesn't appear to set its authenticated session cookies. To resolve the issue in such scenario, set the redirect URLs in the OpenID Connect initialization code and the `Challenge` method (note the trailing slash in the redirect URL):
114+
115+
```csharp
116+
app.UseOpenIdConnectAuthentication(
117+
new OpenIdConnectAuthenticationOptions
118+
{
119+
// Sets the ClientId, authority, RedirectUri as obtained from web.config
120+
ClientId = clientId,
121+
Authority = authority,
122+
RedirectUri = "https://mysite/test2/",
123+
// PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
124+
PostLogoutRedirectUri = redirectUri,
125+
Scope = OpenIdConnectScope.OpenIdProfile,
126+
...
127+
```
128+
129+
```csharp
130+
public void SignIn()
131+
{
132+
if (!Request.IsAuthenticated)
133+
{
134+
HttpContext.GetOwinContext().Authentication.Challenge(
135+
new AuthenticationProperties { RedirectUri = "/test2/" },
136+
OpenIdConnectAuthenticationDefaults.AuthenticationType);
137+
}
138+
}
139+
```
140+
141+
## References
142+
143+
- [Infinite redirects with ASP.NET OWIN and OpenID Connect](https://varnerin.info/infinite-redirects-with-aspnet-owin-and-openid-connect/)
144+
145+
[!INCLUDE [Azure Help Support](../../../includes/azure-help-support.md)]
1.07 MB
Loading

support/entra/entra-id/toc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
href: app-integration/enable-msal4j-logging-spring-boot-webapp.md
6464
- name: Repeated login prompts in iOS MSAL implementation
6565
href: app-integration/repeat-login-prompts-in-msal-ios-app.md
66+
- name: Infinite sign-in loop issue with ASP.NET applications
67+
href: app-integration/asp-dot-net-application-infinite-sign-in-loop.md
6668

6769

6870
- name: Troubleshoot adding apps
@@ -295,7 +297,6 @@
295297
items:
296298
- name: Python scripts making requests are detected as web crawlers
297299
href: users-groups-entra-apis/python-scripts-microsoft-graph-requests-detected-as-web-crawler.md
298-
299300
- name: Microsoft Entra User Provisioning and Synchronization
300301
items:
301302
- name: User Sign-in or password Problems

0 commit comments

Comments
 (0)