Skip to content

Commit ac64192

Browse files
authored
Update 401-unauthorized-aspnet-core-web-api.md
Edit review per CI 4677
1 parent 951ff0a commit ac64192

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

support/entra/entra-id/app-integration/401-unauthorized-aspnet-core-web-api.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Troubleshooting 401 Unauthorized errors in ASP.NET Core Web API with Microsoft Entra ID Authentication
3-
description: Provides guidance on troubleshooting and resolving 401 Unauthorized errors in an ASP.NET Core Web API using Microsoft Entra ID authentication.
2+
title: Troubleshooting 401 Unauthorized Errors in ASP.NET Core Web API with Microsoft Entra ID Authentication
3+
description: Provides guidance for troubleshooting and resolving 401 Unauthorized errors in an ASP.NET Core Web API that uses Microsoft Entra ID authentication.
44
ms.date: 04/28/2025
55
ms.author: bachoang
66
ms.service: entra-id
@@ -9,11 +9,11 @@ ms.custom: sap:Developing or Registering apps with Microsoft identity platform
99

1010
# 401 Unauthorized errors in ASP.NET Core Web API with Microsoft Entra ID
1111

12-
When you call an ASP.NET Core Web API secured with Microsoft Entra ID authentication, you might encounter a 401 Unauthorized error. This article provides guidance on using `JwtBearerEvents to capture detailed logs for troubleshooting these errors.
12+
When you call an ASP.NET Core Web API that's secured by using Microsoft Entra ID authentication, you might encounter a "401 Unauthorized" error. This article provides guidance for using `JwtBearerEvents` to capture detailed logs to troubleshoot these errors.
1313

1414
## Symptoms
1515

16-
You use the `[Authorize]` attribute to [secure your ASP.NET Core Web API](/entra/identity-platform/tutorial-web-api-dotnet-core-build-app?tabs=workforce-tenant) as the following. When you call the web API, a 401 Unauthorized response is returned without any error details.
16+
You use the `[Authorize]` attribute to [secure your ASP.NET Core Web API](/entra/identity-platform/tutorial-web-api-dotnet-core-build-app?tabs=workforce-tenant), as follows:
1717

1818
```csharp
1919
[Authorize]
@@ -39,31 +39,33 @@ public class MyController : ControllerBase
3939
}
4040
```
4141

42+
When you call the web API, a "401 Unauthorized" response is returned, but the message contains no error details.
43+
4244
## Cause
4345

44-
The API might return 401 Unauthorized responses in the following scenarios:
46+
The API might return a "401 Unauthorized" response in the following scenarios:
4547

46-
- The request doesn't include a valid Authorization: Bearer token header.
47-
- Token is expired or incorrect.
48-
- The token being issued for a different resource.
49-
- Token claims not meeting the application's token validation criteria as defined in the [JwtBearerOptions.TokenValidationParameters](/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbeareroptions.tokenvalidationparameters) class.
48+
- The request doesn't include a valid "Authorization: Bearer" token header.
49+
- The token is expired or incorrect:
50+
- The token is issued for a different resource.
51+
- The token claims don't meet the application's token validation criteria, as defined in the [JwtBearerOptions.TokenValidationParameters](/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbeareroptions.tokenvalidationparameters) class.
5052

5153
## Solution
5254

53-
To debug and resolve 401 Unauthorized errors, you can use the `JwtBearerEvents` callbacks to capture and log detailed error information. Follow these steps to implement a custom error handling mechanism.
55+
To debug and resolve "401 Unauthorized" errors, use the `JwtBearerEvents` callbacks to capture and log detailed error information. Follow these steps to implement a custom error-handling mechanism.
5456

55-
The `JwtBearerEvents` class has the following callback properties (invoked in the following order) that can help us debug these 401 Access Denied or UnAuthorization issues:
57+
The `JwtBearerEvents` class has the following callback properties (invoked in the following order) that can help you to debug these "401 Access Denied" or "UnAuthorization" issues:
5658

5759
- [`OnMessageRecieved`](/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbearerevents.onmessagereceived#Microsoft_AspNetCore_Authentication_JwtBearer_JwtBearerEvents_OnMessageReceived) is called first for every request.
58-
- [`OnAuthenticationFailed`](/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbearerevents.onauthenticationfailed) is called when the token doesn't pass the application's token validation criteria.
59-
- [`OnChallenge`](/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbearerevents.onchallenge) is called last before a 401 response is returned.
60+
- [`OnAuthenticationFailed`](/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbearerevents.onauthenticationfailed) is called if the token doesn't pass the application's token validation criteria.
61+
- [`OnChallenge`](/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbearerevents.onchallenge) is called last before a "401" response is returned.
6062

6163
### Step 1: Enable PII logging
6264

63-
By default, personally identifiable information (PII) logging is disabled. Enable it in the Configure method of the Startup.cs file for debugging purposes.
65+
By default, personally identifiable information (PII) logging is disabled. Enable it in the **Configure** method of the Startup.cs file for debugging.
6466

6567
> [!Caution]
66-
> Uses 'Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true' only in development environment for debugging purposes. Do not use it in a production environment.
68+
> Use 'Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true' only in a development environment for debugging. Do not use it in a production environment.
6769
6870
```csharp
6971
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@@ -74,7 +76,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
7476
}
7577
else
7678
{
77-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
79+
// The default HSTS value is 30 days. You might want to change this value for production scenarios. See https://aka.ms/aspnetcore-hsts.
7880
app.UseHsts();
7981
}
8082
// turn on PII logging
@@ -88,7 +90,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
8890

8991
### Step 2: Create a utility method to format exception messages
9092

91-
Add a method to format and flatten exception messages for better readability.
93+
Add a method to format, and flatten any exception messages for better readability:
9294

9395
```csharp
9496
public static string FlattenException(Exception exception)
@@ -103,9 +105,10 @@ public static string FlattenException(Exception exception)
103105
return stringBuilder.ToString();
104106
}
105107
```
108+
106109
### Step 3: Implement JwtBearerEvents callbacks
107110

108-
Configure the `JwtBearerEvents` callbacks in the `ConfigureServices` method of *Startup.cs* to handle authentication events and log error details.
111+
Configure the `JwtBearerEvents` callbacks in the `ConfigureServices` method of *Startup.cs* to handle authentication events and log error details:
109112

110113
```csharp
111114
public void ConfigureServices(IServiceCollection services)
@@ -162,21 +165,19 @@ public void ConfigureServices(IServiceCollection services)
162165
...
163166
}
164167
```
168+
165169
### Sample results
166170

167-
With the implementation, when a 401 Unauthorized error occurs, the response output should include detailed error messages, such as:
171+
With the implementation, when a "401 Unauthorized" error occurs, the response output should include detailed error messages, such as the following:
168172

169173
```Output
170174
OnMessageRecieved:
171175
172176
Authorization Header sent: no Bearer token sent.
173177
```
174178

175-
If you use API development tool to debug the request, you should receive the detail errors such as the following:
176-
177-
:::image type="content" source="media/401-unauthorized-aspnet-core-web-api/wrong-token.png" alt-text="Screenshot of detail error in API development tool." lightbox="media/401-unauthorized-aspnet-core-web-api/wrong-token.png":::
178-
179+
If you use the API development tool to debug the request, you should receive error details, as shown in the following screenshot.
179180

181+
:::image type="content" source="media/401-unauthorized-aspnet-core-web-api/wrong-token.png" alt-text="Screenshot of error details in the API development tool." lightbox="media/401-unauthorized-aspnet-core-web-api/wrong-token.png":::
180182

181183
[!INCLUDE [Azure Help Support](../../../includes/azure-help-support.md)]
182-

0 commit comments

Comments
 (0)