You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: support/entra/entra-id/app-integration/401-unauthorized-aspnet-core-web-api.md
+25-24Lines changed: 25 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
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.
4
4
ms.date: 04/28/2025
5
5
ms.author: bachoang
6
6
ms.service: entra-id
@@ -9,11 +9,11 @@ ms.custom: sap:Developing or Registering apps with Microsoft identity platform
9
9
10
10
# 401 Unauthorized errors in ASP.NET Core Web API with Microsoft Entra ID
11
11
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.
13
13
14
14
## Symptoms
15
15
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:
17
17
18
18
```csharp
19
19
[Authorize]
@@ -39,31 +39,33 @@ public class MyController : ControllerBase
39
39
}
40
40
```
41
41
42
+
When you call the web API, a "401 Unauthorized" response is returned, but the message contains no error details.
43
+
42
44
## Cause
43
45
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:
45
47
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.
50
52
51
53
## Solution
52
54
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 errorhandling 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.
54
56
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:
56
58
57
59
-[`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.
60
62
61
63
### Step 1: Enable PII logging
62
64
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.
64
66
65
67
> [!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.
@@ -162,21 +165,19 @@ public void ConfigureServices(IServiceCollection services)
162
165
...
163
166
}
164
167
```
168
+
165
169
### Sample results
166
170
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:
168
172
169
173
```Output
170
174
OnMessageRecieved:
171
175
172
176
Authorization Header sent: no Bearer token sent.
173
177
```
174
178
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.
179
180
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":::
180
182
181
183
[!INCLUDE [Azure Help Support](../../../includes/azure-help-support.md)]
0 commit comments