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
No example for ASP.NET Core #16002
Comments
@breyed Thank you for the feedback! We will assign this to the author so they can update the document accordingly. |
It would be great to still see an example for ASP.NET Core. Can't seem to access session in my ITelemetryInitializer as it's null. |
Below is how I'm doing it:
|
@aherrick Tried that, and my identity is always empty - there are no claims in the claimprincipal. |
@aherrick thanks! 👏 Our code for reference for others: using Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace OurApp.Web.Services
{
//source: https://github.com/MicrosoftDocs/azure-docs/issues/16002#issuecomment-437146249
public class OurTelemetryInitializer : TelemetryInitializerBase
{
public OurTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor) { }
protected override void OnInitializeTelemetry(HttpContext httpContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
if (httpContext.RequestServices == null)
return;
var userService = httpContext.RequestServices.GetService<IUserService>();
if (userService == null)
return;
var userId = userService.GetUserId();
if (userId == null)
return;
telemetry.Context.User.Id = userId;
telemetry.Context.User.AccountId = userId;
}
}
} |
Same here, the claims principal is always empty. I am using Windows Integrated Authentication, and I can see my custom authorization handlers being triggered. |
adding @cijothomas |
This is what I used. The claims principal has values after the user has logged in as expected. using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Http;
using System.Linq;
using System.Security.Claims;
namespace My.App.Namespace {
public class TelemetryInitializer : ITelemetryInitializer {
private readonly IHttpContextAccessor _httpContextAccessor;
public TelemetryInitializer(
IHttpContextAccessor httpContextAccessor) {
_httpContextAccessor = httpContextAccessor;
}
public void Initialize( ITelemetry telemetry ) {
//Make sure this is a request telemetry entry
if ( telemetry as RequestTelemetry == null ) { return; }
//Get the current http context
HttpContext httpCtx = _httpContextAccessor.HttpContext;
//Check if the user is authenticated
if ( httpCtx?.User?.Identity.IsAuthenticated ?? false ) {
//Add the details to the telemetry
telemetry.Context.User.AuthenticatedUserId = httpCtx.User.Claims.FirstOrDefault( c => c.Type == ClaimTypes.NameIdentifier )?.Value;
}
}
}
} And then in using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Http;
using My.App.Namespace; And in services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<ITelemetryInitializer, TelemetryInitializer>(); |
@rmarskell |
@cijothomas |
Hmm. I don't really understand why it's so difficult to properly add the user ID to the telemetry. Until yesterday, I had assumed this was handled automatically. |
Its easy to add user id to telemetry. The tricky part is retrieving userid from HttpContextAccessor. Also this issue (dotnet/aspnetcore#14975) about HttpContextAccessor. |
I wanted to include User-Agent to all my request telemetry and this worked for me on ASP .NET Core app.
|
@jordanchang |
I have tried all the solutions above and Identity.Name is null, except for database dependency calls. Is there a requirement for what order the services are added to the container? |
I worked this through with Microsoft support and it appears as if the cause in my case was adding the accessor to the container in startup cs. I removed the extra call to add the accessor and it appears to be working now #16002 (comment) |
Can we please have an updated working example for ASP.NET Core written in this doc. page? |
@SamaraSoucy-MSFT Remove me from this, I haven't worked on it in years. |
Thank you for all the feedback. We are actually retiring this doc so I will close this issue. #please-close |
Microsoft.AspNetCore.Http.HttpContext
differs from theHttpContext
in the example.Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: