Skip to content

Commit c86f0fb

Browse files
authored
Update asp-dot-net-open-web-interface-for-dot-net-core-authentication-sign-in-failures.md
1 parent dfdc793 commit c86f0fb

1 file changed

Lines changed: 110 additions & 6 deletions

File tree

support/entra/entra-id/app-integration/asp-dot-net-open-web-interface-for-dot-net-core-authentication-sign-in-failures.md

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Troubleshoot ASP.NET OWIN and ASP.NET Core authentication sign-in failure
33
description: Helps you expose hidden error messages that can guide you toward resolving ASP.NET OWIN and ASP.NET Core authentication sign-in failures with Microsoft Entra ID.
44
ms.reviewer: willfid, v-weizhu
55
ms.service: entra-id
6-
ms.date: 06/25/2025
6+
ms.date: 06/27/2025
77
ms.custom: sap:Developing or Registering apps with Microsoft identity platform
88
---
99
# Troubleshoot ASP.NET OWIN and ASP.NET Core authentication sign-in failures with Microsoft Entra ID
@@ -27,15 +27,119 @@ To expose hidden errors during the sign-in process, use the `OnAuthenticationFai
2727

2828
### For ASP.NET OWIN
2929

30-
Ensure your code for handling the `AuthenticationFailed` event in the *Startup.Auth.cs* file follows a structure similar to the following:
30+
Ensure your code for the `AuthenticationFailed` event in the *Startup.Auth.cs* file follows this structure:
3131

32-
[ASPNET\_OWIN\_OnAuthenticationFailed.cs](https://gist.github.com/ms-willfid/813dd19091dfa8650895182cb45d5d1c)
32+
```csharp
33+
public void ConfigureAuth(IAppBuilder app)
34+
{
35+
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
36+
37+
app.UseCookieAuthentication(new CookieAuthenticationOptions());
38+
39+
app.UseOpenIdConnectAuthentication(
40+
new OpenIdConnectAuthenticationOptions
41+
{
42+
ResponseType = OpenIdConnectResponseType.CodeIdToken,
43+
ClientId = clientId,
44+
Authority = Authority,
45+
//...
46+
47+
Notifications = new OpenIdConnectAuthenticationNotifications()
48+
{
49+
// If there is a code in the OpenID Connect response, redeem it for an access token
50+
AuthorizationCodeReceived = (context) =>
51+
{
52+
// ...
53+
},
54+
55+
// On Authentication Failed
56+
AuthenticationFailed = (context) =>
57+
{
58+
String ErrorMessage = context.Exception.Message;
59+
String InnerErrorMessage = String.Empty;
60+
61+
String RedirectError = String.Format("error_message={0}", ErrorMessage);
62+
63+
if (context.Exception.InnerException != null)
64+
{
65+
InnerErrorMessage = context.Exception.InnerException.Message;
66+
RedirectError = String.Format("{0}&inner_error={1}", RedirectError, InnerErrorMessage);
67+
}
68+
69+
// or you can just throw it
70+
       // throw new Exception(RedirectError);
71+
72+
RedirectError = RedirectError.Replace("\r\n", " ");
73+
74+
context.Response.Redirect("/?" + RedirectError);
75+
context.HandleResponse();
76+
return Task.FromResult(0);
77+
}
78+
}
79+
80+
});
81+
82+
// ...
83+
```
3384

3485
### For ASP.NET Core
3586

36-
Ensure your code for handling the `AuthenticationFailed` event in the *Startup.cs* file follows a structure similar to the following:
87+
Ensure your code for the `AuthenticationFailed` event in the *Startup.cs* file follows this structure:
88+
89+
```csharp
90+
public void ConfigureServices(IServiceCollection services)
91+
{
92+
services.Configure<CookiePolicyOptions>(options =>
93+
{
94+
// ...
95+
});
96+
97+
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
98+
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
99+
100+
// ...
101+
102+
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
103+
{
104+
options.Authority = options.Authority;
105+
106+
// Token Validation
107+
options.TokenValidationParameters.IssuerValidator = AadIssuerValidator.ValidateAadIssuer;
108+
109+
// Response type
110+
options.ResponseType = "id_token code";
111+
112+
// On Authorization Code Received
113+
options.Events.OnAuthorizationCodeReceived = async context =>
114+
{
115+
// ...
116+
};
117+
118+
// On Authentication Failed
119+
options.Events.OnAuthenticationFailed = async context =>
120+
{
121+
String ErrorMessage = context.Exception.Message;
122+
String InnerErrorMessage = String.Empty;
123+
124+
String RedirectError = String.Format("?error_message={0}", ErrorMessage);
125+
126+
if (context.Exception.InnerException != null)
127+
{
128+
InnerErrorMessage = context.Exception.InnerException.Message;
129+
RedirectError = String.Format("{0}&inner_error={1}", RedirectError, InnerErrorMessage);
130+
}
131+
132+
       // or you can just throw it
133+
       // throw new Exception(RedirectError);
134+
135+
RedirectError = RedirectError.Replace("\r\n", " ");
136+
137+
context.Response.Redirect(RedirectError);
138+
context.HandleResponse();
139+
};
37140

38-
[ASPNETCore\_Auth\_OnAuthenticationFailed.cs](https://gist.github.com/ms-willfid/813dd19091dfa8650895182cb45d5d1c)
141+
// ...
142+
```
39143

40144
You can modify this to send the error message to your logs or send it to a custom error page. At a minimum, the error message should be displayed in the browser's address bar.
41145

@@ -52,4 +156,4 @@ For more information about using Fiddler, see [Collect HTTPS traffic using Fiddl
52156
For a list of Microsoft Entra authentication and authorization errors, see [Microsoft Entra authentication and authorization error codes](/entra/identity-platform/reference-error-codes).
53157

54158

55-
[!INCLUDE [Azure Help Support](../../../includes/azure-help-support.md)]
159+
[!INCLUDE [Azure Help Support](../../../includes/azure-help-support.md)]

0 commit comments

Comments
 (0)