Reputation: 40012
I am using Application Insights to gather telemetry for an API. Only authenticated clients (like a mobile app) can access the resources of the API. This is using the OAuth2 client credentials flow. There are no users in the system.
Would it make sense to set client_id
(which actually doesn't represent a user but rather an "authenticated application or device") to the AuthenticatedUserId
value in the user context?
Currently we are storing the client_id
as a custom field, but I've just discovered that there is a "user" section that is sent within the context of telemetry.
public class ClientIdTelemetryInitialiser : TelemetryInitializerBase
{
public ClientIdTelemetryInitialiser(IHttpContextAccessor contextAccessor)
: base(contextAccessor)
{ }
protected override void OnInitializeTelemetry(
HttpContext platformContext,
RequestTelemetry rootRequestTelemetry,
ITelemetry telemetry)
{
var claims = platformContext.User.Claims;
var clientId = claims.SingleOrDefault(x => x.Type.Equals("client_id", StringComparison.InvariantCultureIgnoreCase));
// Old way of doing it
rootRequestTelemetry.Context.Properties["client_id"] = clientId.Value;
// New way of doing it??
rootRequestTelemetry.Context.User.AuthenticatedUserId = clientId.Value;
}
}
Upvotes: 0
Views: 679
Reputation: 25126
Yes, you can use the context.user
id fields to use whatever kind of id you want.
if you use those fields, some of the "user" based charts in the portal should light up for you if they weren't showing anything before.
there's also a user.AnonymousId that you could use for tracking things prior to a user becoming authenticated, if there were such a thing.
Upvotes: 1