来自ASP.NET Core身份的Application Insights用户ID [英] Application Insights User ID from ASP.NET Core identity
问题描述
我正在使用.NET Core 3.1.我想用ASP.NET Core身份用户名填充Application Insights中的User ID
属性.我收到了这篇文章由Microsoft提供,但适用于完整的.NET Framework.在此处 (我将声明更改为身份)
I am using .NET Core 3.1. I want to populate the User ID
property in Application Insights with the ASP.NET Core identity username. I got this article by Microsoft, but it is for the full .NET Framework. I also tried the following code given here (I changed claim to Identity)
public class TelemetryEnrichment : TelemetryInitializerBase
{
public TelemetryEnrichment(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
{
}
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
telemetry.Context.User.AuthenticatedUserId =
platformContext.User?.Identity.Name ?? string.Empty;
}
}
,但不知道如何将其包括在管道中.我尝试了services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>();
,但没有帮助. Application Insights仍在显示其自己的用户ID.
but don't know how to include it in the pipeline. I tried services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>();
but it doesn't help. Application Insights is still showing its own User ID.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>(
options => {
options.Stores.MaxLengthForKeys = 128;
options.User.RequireUniqueEmail = true;
options.Password.RequiredLength = 8;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddRoles<ApplicationRole>()
.AddSignInManager<ApplicationSignInManager>();
services.AddControllersWithViews()
.AddNewtonsoftJson()
.AddRazorRuntimeCompilation();
services.AddRazorPages();
services.AddMvc(mvcOptions =>
{
mvcOptions.ModelBinderProviders.Insert(0, new ModelBinderProvider());
})
.AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
})
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddAntiforgery(options => options.HeaderName = "XSRF-TOKEN");
services.AddFlashMessage();
services.AddSingleton<IEmailUtility, EmailUtility>();
services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddApplicationInsightsTelemetry();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>();
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
var mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
}
推荐答案
回答我自己的问题以确保完整性.问题中给出的代码可以正常工作.我会再把它放在这里.
Answering my own question for completeness. The code given in the question works perfectly fine. I will just put it here again.
要在Application Insights中获取ASP.NET Core身份用户名,您需要自定义遥测初始化程序请参考@PeterBons的原始答案
To get the ASP.NET Core identity username in your Application Insights, you need a custom telemetry initializer Please refer to original answer by @PeterBons here
public class TelemetryEnrichment : TelemetryInitializerBase
{
public TelemetryEnrichment(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
{
}
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
telemetry.Context.User.AuthenticatedUserId =
platformContext.User?.Identity.Name ?? string.Empty;
}
}
然后,您需要在 Startup.cs ConfigureServices
中进行注册,如下所示:
Then you need to register it in your Startup.cs ConfigureServices
as below:
public void ConfigureServices(IServiceCollection services)
{
...
...
services.AddApplicationInsightsTelemetry();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<ITelemetryInitializer, TelemetryEnrichment>();
...
...
}
请记住将AddApplicationInsightsTelemetry()
放在AddSingleton()
设置完成后,身份用户名将显示在Application Insights的Auth User ID
属性下.
Once this is set up, the identity username is displayed under Auth User ID
property in Application Insights.
这篇关于来自ASP.NET Core身份的Application Insights用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!