2

我正在尝试解决如何配置发生的日志身份验证事件是 ASP.NET Core 6.0 中的 MicrosoftIdentityWeb,但我无法找到一个简单的示例。

我的代码目前是 .net60 模板代码,我尝试按照前面的示例添加日志记录,但无法使其正常工作:

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;

var builder = WebApplication.CreateBuilder(args);

IConfiguration opt = builder.Configuration.GetSection("AzureAd");

// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    // Was: .AddMicrosoftIdentityWebApi(Configuration);
    .AddMicrosoftIdentityWebApi(options =>
    {
        // Handle Auth events and bind Configuration here somehow?
    });

...

如何绑定配置并订阅身份验证事件,以便了解身份验证失败的原因并出于安全目的记录身份验证尝试?

4

1 回答 1

1

第一步是按照博客文章将日志记录添加到您的应用程序。

然后你需要像这样调整登录级别:

.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("IdentityServer4", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore.DataProtection", LogEventLevel.Debug)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore.Authorization", LogEventLevel.Information);

有关更多详细信息,请参阅此处的源代码示例:

如果这还不够,您可以使用 Windows 上的 System.Diagnostics.Tracing 子系统获取更多详细信息,该子系统将向 Windows 事件跟踪 (ETW) 发送数据。

于 2021-11-27T18:48:51.863 回答