asp.net-core - 客户端中的 IdentityServer 4 身份验证事件

我将 IdentityServer 4 服务器设置为独立应用程序,使用 net core 3.1、针对 MySql 的 Entity Framework core 和 Net Core Identity 作为用户存储。另外,我有一个 Razor Pages 客户端应用程序,它针对 Identity Server 进行身份验证,用户登录发生在服务器上。所有这些都运行良好。

我现在希望能够在客户端上为任何新用户身份验证或登录失败写入日志条目。我假设某处一定有事件发生。请问我该怎么做?

最佳答案

我有同样的要求,例如在审计日志中存储身份验证失败 - 如果有人试图使用无效 token 访问 api。

我发现 IdentityServerAuthenticationOptions 具有 JwtBearerEvents 属性,可用于通知此类事件。

在我的例子中,它是这样工作的:

.AddAuthentication(AUTH_SCHEME).AddIdentityServerAuthentication(AUTH_SCHEME, options => {
    options.ApiName = config.ApiName;
    options.Authority = config.Address;
    options.JwtBearerEvents = new JwtBearerEvents
    {
        OnAuthenticationFailed = c =>
        {
            // Log can bo done here
            return Task.CompletedTask;
        },
        OnTokenValidated = c =>
        {
            // Log can bo done here
            return Task.CompletedTask;
        },
        OnChallenge = c =>
        {
            // Log can bo done here
            return Task.CompletedTask;
        },
        OnForbidden = c =>
        {
            // Log can bo done here
            return Task.CompletedTask;
        }
    };
    options.SupportedTokens = SupportedTokens.Jwt;});

我担心它会覆盖正常的 IS 行为,但一切似乎都像以前一样工作。

你可以在那里访问适当的上下文,例如在 JwtBearerChallengeContext 中你可以读取 ErrorErrorDescription 字符串甚至整个 HttpContext

https://stackoverflow.com/questions/63833623/

相关文章:

android - 约束布局 : center view on full screen but li

typescript - Deno 记录器 FileHandler 不写入文件

c# - 连接池中处于 TIME_WAIT 状态的许多连接,在 WebApi 中使用静态或单个 Ht

amazon-web-services - 如何在本地运行和测试Cloud-init?

c# - 在 Unity 中使用异步等待

go - 我可以将 volume 挂载到 Katib Experiment 吗?

python - Anki 网页抓取脚本

java - 添加用户输入升序的代码

firebase - 使用 firebase 时 Google 应用程序脚本中的服务器错误

.net - Visual Studio 调试时未找到 : ScriptLoggingBuilder