gpt4 book ai didi

c# - 在 ASP.NET Core 6.0 中使用 Microsoft.Identity.Web 记录身份验证事件

转载 作者:行者123 更新时间:2023-12-05 04:41:16 26 4
gpt4 key购买 nike

我正在尝试弄清楚如何配置在 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?
});

...

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

最佳答案

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

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

.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);

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

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

关于c# - 在 ASP.NET Core 6.0 中使用 Microsoft.Identity.Web 记录身份验证事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70118397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com