This Set combines some Snipps for tracking authenticated users with Application Insights.
See also Configurations for Application Insights.
There are two options to track authenticated users.
EnableAuthenticationTrackingJavaScript
configuration to true
in appsettings.json. With this, the default TelemetryInitializers will set the Auth Id with the username for Page View events.appInsights.setAuthenticatedUserContext(userName, null, true)
to set the ai_authuser cookie on the client-side.Application Insights API for custom events and metrics - Azure Monitor | Microsoft Docs
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
The following list shows helpful resources for tracking users in Application Insights
General Tipps for Application Insights Portal View