Skip to content

Commit 11229a5

Browse files
committed
Adding the JwtBearer middleware diagnostics (in addition to the OpenId Connect middleware)
1 parent 9c18298 commit 11229a5

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
2+
using System;
3+
using System.Diagnostics;
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.Identity.Web.Resource
7+
{
8+
/// <summary>
9+
/// Diagnostics for the JwtBearer middleware (used in Web APIs)
10+
/// </summary>
11+
public class JwtBearerMiddlewareDiagnostics
12+
{
13+
/// <summary>
14+
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
15+
/// </summary>
16+
static Func<AuthenticationFailedContext, Task> onAuthenticationFailed;
17+
18+
/// <summary>
19+
/// Invoked when a protocol message is first received.
20+
/// </summary>
21+
static Func<MessageReceivedContext, Task> onMessageReceived;
22+
23+
/// <summary>
24+
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
25+
/// </summary>
26+
static Func<TokenValidatedContext, Task> onTokenValidated;
27+
28+
/// <summary>
29+
/// Invoked before a challenge is sent back to the caller.
30+
/// </summary>
31+
static Func<JwtBearerChallengeContext, Task> onChallenge;
32+
33+
/// <summary>
34+
/// Subscribes to all the JwtBearer events, to help debugging, while
35+
/// preserving the previous handlers (which are called)
36+
/// </summary>
37+
/// <param name="events">Events to subscribe to</param>
38+
public static JwtBearerEvents Subscribe(JwtBearerEvents events)
39+
{
40+
if (events == null)
41+
{
42+
events = new JwtBearerEvents();
43+
}
44+
45+
onAuthenticationFailed = events.OnAuthenticationFailed;
46+
events.OnAuthenticationFailed = OnAuthenticationFailed;
47+
48+
onMessageReceived = events.OnMessageReceived;
49+
events.OnMessageReceived = OnMessageReceived;
50+
51+
onTokenValidated = events.OnTokenValidated;
52+
events.OnTokenValidated = OnTokenValidated;
53+
54+
onChallenge = events.OnChallenge;
55+
events.OnChallenge = OnChallenge;
56+
57+
return events;
58+
}
59+
60+
static async Task OnMessageReceived(MessageReceivedContext context)
61+
{
62+
Debug.WriteLine($"1. Begin {nameof(OnMessageReceived)}");
63+
// Place a breakpoint here and examine the bearer token (context.Request.Headers.HeaderAuthorization / context.Request.Headers["Authorization"])
64+
// Use https://jwt.ms to decode the token and observe claims
65+
await onMessageReceived(context);
66+
Debug.WriteLine($"1. End - {nameof(OnMessageReceived)}");
67+
}
68+
69+
static async Task OnAuthenticationFailed(AuthenticationFailedContext context)
70+
{
71+
Debug.WriteLine($"99. Begin {nameof(OnAuthenticationFailed)}");
72+
// Place a breakpoint here and examine context.Exception
73+
await onAuthenticationFailed(context);
74+
Debug.WriteLine($"99. End - {nameof(OnAuthenticationFailed)}");
75+
}
76+
77+
static async Task OnTokenValidated(TokenValidatedContext context)
78+
{
79+
Debug.WriteLine($"2. Begin {nameof(OnTokenValidated)}");
80+
await onTokenValidated(context);
81+
Debug.WriteLine($"2. End - {nameof(OnTokenValidated)}");
82+
}
83+
84+
static async Task OnChallenge(JwtBearerChallengeContext context)
85+
{
86+
Debug.WriteLine($"55. Begin {nameof(OnChallenge)}");
87+
await onChallenge(context);
88+
Debug.WriteLine($"55. End - {nameof(OnChallenge)}");
89+
}
90+
}
91+
}

Microsoft.Identity.Web/Resource/OpenIdConnectMiddlewareDiagnostics.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
namespace Microsoft.Identity.Web.Resource
88
{
9+
/// <summary>
10+
/// Diagnostics used in the Open Id Connect middleware
11+
/// (used in Web Apps)
12+
/// </summary>
913
public class OpenIdConnectMiddlewareDiagnostics
1014
{
1115
//

0 commit comments

Comments
 (0)