Krumelur
Krumelur

Reputation: 32497

Setting user name for Request event?

I have implemented a custom authentication scheme in a web service based on the ASP.NET Core webhost. I want to add Application Insights to this service.

When I successfully authenticate the user, I do something like this

telemetry.Context.User.Id = authenticatedUserName;

the telemetry object is the TelemetryClient I get from dependency injection.

Now, the problem is that the user ID does not show up among the requests, and I am not sure why.

This works

customEvents | where user_Id != "" and name  == "MyCustomEvent"

but not this

request | where user_Id != ""

or this

dependencies | where user_Id != ""

Is there somewhere else where I should set the user ID for the request? I'd rather not create a custom event just for this.

I also tried setting the User property on the HttpContext object, but it does not seem to have any effect.

Upvotes: 3

Views: 936

Answers (2)

Krumelur
Krumelur

Reputation: 32497

Actually, the answer was surprisingly simple.

HttpContext ctx = ...
var requestTelemetry = ctx.Features.Get<RequestTelemetry>()

requestTelemetry.Context.User.Id = authenticationResult.UserName;

Upvotes: 1

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

You should use ITelemetryInitializer for your purpose.

The following is my test steps(asp.net core 2.1):

Step 1:Add the Aplication Insights telemetry by right click your project -> Add -> Application Insights telemetry. The screenshot as below: enter image description here

Step 2:Add a new class which implements the ITelemetryInitializer:

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace WebApplication33netcore
{
    public class MyTelemetryInitializer: ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var request = telemetry as RequestTelemetry;
            if (request != null)
            {
                //set the user id here with your custom value
                request.Context.User.Id = "ivan111";
            }
        }

    }
}

Step 3:Register your telemetry initializer in ConfigureServices method in Startup.cs. For details, refer to here:

     public void ConfigureServices(IServiceCollection services)
     {
         services.Configure<CookiePolicyOptions>(options =>
         {

          options.CheckConsentNeeded = context => true;
          options.MinimumSameSitePolicy = SameSiteMode.None;
         });


         services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

         //Add the following code to register your telemetry initializer
         services.AddSingleton<ITelemetryInitializer>(new MyTelemetryInitializer());
    }

Step 4:Check the test result:

In visual studio Application Insights Search: enter image description here

Then check it in Analytics: enter image description here

Upvotes: 6

Related Questions