Track Authenticated Users in Application Insights
This Set combines some Snipps for tracking authenticated users with Application Insights.
See also Configurations for Application Insights.
General Tipps for Application Insights Portal View
- Some views and blades are cached, and you will not see any updates. You need to refresh the page to see the updated calculation.
The following code snippet shows how to configure AddApplication Insights Telemetry.
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSingleton<ITelemetryInitializer, CustomAppInsightsInitializer>(); // server side to track the requests and dependencies
ApplicationInsightsServiceOptions options = new ApplicationInsightsServiceOptions()
{
EnableAuthenticationTrackingJavaScript = true, // enable client side to track pageviews
ConnectionString = "InstrumentationKey={key};IngestionEndpoint=https://westeurope-1.in.applicationinsights.azure.com/"
};
services.AddApplicationInsightsTelemetry();
services.AddRazorPages(options =>
// ...
}
The setAuthenticatedUserContext API has optional parameters. When setting storeInCookie to true, the ai_authuser cookie is set, so every request is sent with the authenticatedUserId.
But when using appInsights.setAuthenticatedUserContext(userName, null, true)
the back-end side in ASP.NET Core doesn't set the Auth Id context property with the default initializers (in this case the AuthenticatedUserIdTelemetryInitializer).
Some additional discussion can be found at Add option EnableAuthenticationTracking to add AuthenticatedUserId to telemetries · Issue #1431 · microsoft/ApplicationInsights-dotnet · GitHub
There are two options to track authenticated users.
Option 1
- Set
EnableAuthenticationTrackingJavaScript
configuration totrue
in appsettings.json. With this, the default TelemetryInitializers will set the Auth Id with the username for Page View events. - Implement a TelemetryInitializer to set the Auth Id context property from the back-end side for all the other events.
- Register the custom TelemetryInitializer and move app.UseAuthentication() to the position in the pipeline where you want to track.
Option 2
- Use
appInsights.setAuthenticatedUserContext(userName, null, true)
to set the ai_authuser cookie on the client-side. - Implement a TelemetryInitializer to read the cookie on the backend-side and set the Auth Id context property
- Register the custom TelemetryInitializer
Application Insights API for custom events and metrics - Azure Monitor | Microsoft Docs
The following list shows helpful resources for tracking users in Application Insights
- appInsights.setAuthenticatedUserContext is not a function - how to get authenticated user context on automatic telemetry? · Issue #218 · microsoft/ApplicationInsights-aspnetcore · GitHub
- asp.net mvc - Azure - App Insights - how to track the logged-in Username in Auth Id? - Stack Overflow
- Tracking Authenticated User ID in Application Insights (matthew-glace.blogspot.com)
Comments