1. <i id='SbVIr'><tr id='SbVIr'><dt id='SbVIr'><q id='SbVIr'><span id='SbVIr'><b id='SbVIr'><form id='SbVIr'><ins id='SbVIr'></ins><ul id='SbVIr'></ul><sub id='SbVIr'></sub></form><legend id='SbVIr'></legend><bdo id='SbVIr'><pre id='SbVIr'><center id='SbVIr'></center></pre></bdo></b><th id='SbVIr'></th></span></q></dt></tr></i><div id='SbVIr'><tfoot id='SbVIr'></tfoot><dl id='SbVIr'><fieldset id='SbVIr'></fieldset></dl></div>
    <legend id='SbVIr'><style id='SbVIr'><dir id='SbVIr'><q id='SbVIr'></q></dir></style></legend>
    • <bdo id='SbVIr'></bdo><ul id='SbVIr'></ul>
      <tfoot id='SbVIr'></tfoot>

      <small id='SbVIr'></small><noframes id='SbVIr'>

    1. 如何在 .net 核心中记录授权尝试

      时间:2023-06-03
        <tbody id='6tZro'></tbody>
      <tfoot id='6tZro'></tfoot><legend id='6tZro'><style id='6tZro'><dir id='6tZro'><q id='6tZro'></q></dir></style></legend>

            <small id='6tZro'></small><noframes id='6tZro'>

              <bdo id='6tZro'></bdo><ul id='6tZro'></ul>
            • <i id='6tZro'><tr id='6tZro'><dt id='6tZro'><q id='6tZro'><span id='6tZro'><b id='6tZro'><form id='6tZro'><ins id='6tZro'></ins><ul id='6tZro'></ul><sub id='6tZro'></sub></form><legend id='6tZro'></legend><bdo id='6tZro'><pre id='6tZro'><center id='6tZro'></center></pre></bdo></b><th id='6tZro'></th></span></q></dt></tr></i><div id='6tZro'><tfoot id='6tZro'></tfoot><dl id='6tZro'><fieldset id='6tZro'></fieldset></dl></div>

              1. 本文介绍了如何在 .net 核心中记录授权尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                当我尝试访问授权属性下的方法时,我正在尝试写入日志.基本上,我想记录一个人是否使用了无效令牌或过期令牌.我正在使用 JWT 的基本身份验证

                I'm trying to write to a log when I person tries to access a method under an Authorize Attribute. Basically, I want to log if a person uses an invalid token or an expired token. I'm using basic Authentication for JWT

                services.AddAuthentication(o =>
                {
                    o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                    o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                }).AddJwtBearer(cfg =>
                    {
                        cfg.RequireHttpsMetadata = false;
                        cfg.SaveToken = true;
                
                        cfg.TokenValidationParameters = new TokenValidationParameters()
                        {
                            ValidAudience = jwtAudience,
                            ValidIssuer = jwtIssuer,
                            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecurityKey))
                        };
                
                    });
                

                有没有办法我可以在授权检查中添加一段代码,以记录授权尝试是否有效以及为什么无效?

                Is there a way I can add a piece of code to the authorization check that logs if a authorization attempt was valid and why it wasn't?

                推荐答案

                您可以访问 JwtBearerEvents 对象,该对象定义了在处理不记名令牌时引发的许多事件.

                You have access to the JwtBearerEvents object, which defines a number of events that are raised as the bearer token is processed.

                验证失败
                如果在请求处理期间抛出异常,则调用.除非被抑制,否则异常将在此事件之后重新抛出.

                OnAuthenticationFailed
                Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.

                挑战在将质询发送回调用方之前调用.

                OnChallenge Invoked before a challenge is sent back to the caller.

                OnMessageReceived
                在第一次收到协议消息时调用.

                OnMessageReceived
                Invoked when a protocol message is first received.

                OnTokenValidated
                在安全令牌通过验证并生成 ClaimsIdentity 后调用.

                OnTokenValidated
                Invoked after the security token has passed validation and a ClaimsIdentity has been generated.

                https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbearerevents?view=aspnetcore-2.0

                在AddJwtBearer初始化配置时,添加你想订阅的事件,

                When initialising the configuration at AddJwtBearer, add the events you'd like to subscribe to,

                .AddJwtBearer(o =>
                {
                    o.Events = new JwtBearerEvents()
                    {
                        OnAuthenticationFailed = c =>
                        {
                            // do some logging or whatever...
                        }
                
                    };
                });
                

                查看源代码以了解何时可能引发事件,

                Have a look at the source to see when events might be raised,

                https://github.com/aspnet/Security/blob/dev/src/Microsoft.AspNetCore.Authentication.JwtBearer/JwtBearerHandler.cs

                这篇关于如何在 .net 核心中记录授权尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:无法访问 JWT 令牌 .NET Core 中的角色 下一篇:ASP.Net Identity 内置函数与 ASP.Net Core 中的自定义表

                相关文章

                最新文章

                <small id='LBAiC'></small><noframes id='LBAiC'>

                  <bdo id='LBAiC'></bdo><ul id='LBAiC'></ul>
                1. <legend id='LBAiC'><style id='LBAiC'><dir id='LBAiC'><q id='LBAiC'></q></dir></style></legend>

                  1. <tfoot id='LBAiC'></tfoot>

                  2. <i id='LBAiC'><tr id='LBAiC'><dt id='LBAiC'><q id='LBAiC'><span id='LBAiC'><b id='LBAiC'><form id='LBAiC'><ins id='LBAiC'></ins><ul id='LBAiC'></ul><sub id='LBAiC'></sub></form><legend id='LBAiC'></legend><bdo id='LBAiC'><pre id='LBAiC'><center id='LBAiC'></center></pre></bdo></b><th id='LBAiC'></th></span></q></dt></tr></i><div id='LBAiC'><tfoot id='LBAiC'></tfoot><dl id='LBAiC'><fieldset id='LBAiC'></fieldset></dl></div>