Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding diagnostics #35

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions Extensions/OpenIdConnectMiddlewareDiagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Authentication
{
public class OpenIdConnectMiddlewareDiagnostics
{
//
// Summary:
// Invoked before redirecting to the identity provider to authenticate. This can
// be used to set ProtocolMessage.State that will be persisted through the authentication
// process. The ProtocolMessage can also be used to add or customize parameters
// sent to the identity provider.
static Func<RedirectContext, Task> onRedirectToIdentityProvider;
//
// Summary:
// Invoked when a protocol message is first received.
static Func<MessageReceivedContext, Task> onMessageReceived;
//
// Summary:
// Invoked after security token validation if an authorization code is present in
// the protocol message.
static Func<AuthorizationCodeReceivedContext, Task> onAuthorizationCodeReceived;
//
// Summary:
// Invoked after "authorization code" is redeemed for tokens at the token endpoint.
static Func<TokenResponseReceivedContext, Task> onTokenResponseReceived;
//
// Summary:
// Invoked when an IdToken has been validated and produced an AuthenticationTicket.
static Func<TokenValidatedContext, Task> onTokenValidated;
//
// Summary:
// Invoked when user information is retrieved from the UserInfoEndpoint.
static Func<UserInformationReceivedContext, Task> onUserInformationReceived;
//
// Summary:
// Invoked if exceptions are thrown during request processing. The exceptions will
// be re-thrown after this event unless suppressed.
static Func<AuthenticationFailedContext, Task> onAuthenticationFailed;
//
// Summary:
// Invoked when a request is received on the RemoteSignOutPath.
static Func<RemoteSignOutContext, Task> onRemoteSignOut;
//
// Summary:
// Invoked before redirecting to the identity provider to sign out.
static Func<RedirectContext, Task> onRedirectToIdentityProviderForSignOut;
//
// Summary:
// Invoked before redirecting to the Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectOptions.SignedOutRedirectUri
// at the end of a remote sign-out flow.
static Func<RemoteSignOutContext, Task> onSignedOutCallbackRedirect;


/// <summary>
/// Subscribes to all the OpenIdConnect events, to help debugging, while
/// preserving the previous handlers (which are called)
/// </summary>
/// <param name="events">Events to subscribe to</param>
public static void Subscribe(OpenIdConnectEvents events)
{
onRedirectToIdentityProvider = events.OnRedirectToIdentityProvider;
events.OnRedirectToIdentityProvider = OnRedirectToIdentityProvider;

onMessageReceived = events.OnMessageReceived;
events.OnMessageReceived = OnMessageReceived;

onAuthorizationCodeReceived = events.OnAuthorizationCodeReceived;
events.OnAuthorizationCodeReceived = OnAuthorizationCodeReceived;

onTokenResponseReceived = events.OnTokenResponseReceived;
events.OnTokenResponseReceived = OnTokenResponseReceived;

onTokenValidated = events.OnTokenValidated;
events.OnTokenValidated = OnTokenValidated;

onUserInformationReceived = events.OnUserInformationReceived;
events.OnUserInformationReceived = OnUserInformationReceived;

onAuthenticationFailed = events.OnAuthenticationFailed;
events.OnAuthenticationFailed = OnAuthenticationFailed;

onRemoteSignOut = events.OnRemoteSignOut;
events.OnRemoteSignOut = OnRemoteSignOut;

onRedirectToIdentityProviderForSignOut = events.OnRedirectToIdentityProviderForSignOut;
events.OnRedirectToIdentityProviderForSignOut = OnRedirectToIdentityProviderForSignOut;

onSignedOutCallbackRedirect = events.OnSignedOutCallbackRedirect;
events.OnSignedOutCallbackRedirect = OnSignedOutCallbackRedirect;
}

static async Task OnRedirectToIdentityProvider(RedirectContext context)
{
Debug.WriteLine($"1. Begin {nameof(OnRedirectToIdentityProvider)}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, @bgavrilMS this would be better.


await onRedirectToIdentityProvider(context);

Debug.WriteLine($" Sending OpenIdConnect message:");
DisplayProtocolMessage(context.ProtocolMessage);
Debug.WriteLine($"1. End - {nameof(OnRedirectToIdentityProvider)}");
}

private static void DisplayProtocolMessage(OpenIdConnectMessage message)
{
foreach (var property in message.GetType().GetProperties())
{
object value = property.GetValue(message);
if (value != null)
{
Debug.WriteLine($" - {property.Name}={value}");
}
}
}

static async Task OnMessageReceived(MessageReceivedContext context)
{
Debug.WriteLine($"2. Begin {nameof(OnMessageReceived)}");
Debug.WriteLine($" Received from STS the OpenIdConnect message:");
DisplayProtocolMessage(context.ProtocolMessage);
await onMessageReceived(context);
Debug.WriteLine($"2. End - {nameof(OnMessageReceived)}");
}

static async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
Debug.WriteLine($"4. Begin {nameof(OnAuthorizationCodeReceived)}");
await onAuthorizationCodeReceived(context);
Debug.WriteLine($"4. End - {nameof(OnAuthorizationCodeReceived)}");
}

static async Task OnTokenResponseReceived(TokenResponseReceivedContext context)
{
Debug.WriteLine($"5. Begin {nameof(OnTokenResponseReceived)}");
await onTokenResponseReceived(context);
Debug.WriteLine($"5. End - {nameof(OnTokenResponseReceived)}");

}
static async Task OnTokenValidated(TokenValidatedContext context)
{
Debug.WriteLine($"3. Begin {nameof(OnTokenValidated)}");
await onTokenValidated(context);
Debug.WriteLine($"3. End - {nameof(OnTokenValidated)}");
}
static async Task OnUserInformationReceived(UserInformationReceivedContext context)
{
Debug.WriteLine($"6. Begin {nameof(OnUserInformationReceived)}");
await onUserInformationReceived(context);
Debug.WriteLine($"6. End - {nameof(OnUserInformationReceived)}");
}

static async Task OnAuthenticationFailed(AuthenticationFailedContext context)
{
Debug.WriteLine($"99. Begin {nameof(OnAuthenticationFailed)}");
await onAuthenticationFailed(context);
Debug.WriteLine($"99. End - {nameof(OnAuthenticationFailed)}");
}

static async Task OnRedirectToIdentityProviderForSignOut(RedirectContext context)
{
Debug.WriteLine($"10. Begin {nameof(OnRedirectToIdentityProviderForSignOut)}");
await onRedirectToIdentityProviderForSignOut(context);
Debug.WriteLine($"10. End - {nameof(OnRedirectToIdentityProviderForSignOut)}");
}
static async Task OnRemoteSignOut(RemoteSignOutContext context)
{
Debug.WriteLine($"11. Begin {nameof(OnRemoteSignOut)}");
await onRemoteSignOut(context);
Debug.WriteLine($"11. End - {nameof(OnRemoteSignOut)}");
}
static async Task OnSignedOutCallbackRedirect(RemoteSignOutContext context)
{
Debug.WriteLine($"12. Begin {nameof(OnSignedOutCallbackRedirect)}");
await onSignedOutCallbackRedirect(context);
Debug.WriteLine($"12. End {nameof(OnSignedOutCallbackRedirect)}");
}
}
}
5 changes: 5 additions & 0 deletions Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public void ConfigureServices(IServiceCollection services)
context.ProtocolMessage.SetParameter(claims, context.Properties.Items[claims]);
}
};

// If you want to debug, or just understand the OpenIdConnect events, just
// uncomment the following line of code
// OpenIdConnectMiddlewareDiagnostics.Subscribe(options.Events);

});

services.AddMvc(options =>
Expand Down
2 changes: 1 addition & 1 deletion WebApp-OpenIDConnect-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.1.1" />
<PackageReference Include="Microsoft.Identity.Client" Version="2.5.0-preview" />
<PackageReference Include="Microsoft.Identity.Client" Version="2.6.0-preview" />
</ItemGroup>

</Project>