1. <legend id='Rn4J0'><style id='Rn4J0'><dir id='Rn4J0'><q id='Rn4J0'></q></dir></style></legend>
    2. <tfoot id='Rn4J0'></tfoot>

      • <bdo id='Rn4J0'></bdo><ul id='Rn4J0'></ul>
    3. <small id='Rn4J0'></small><noframes id='Rn4J0'>

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

        使用 OWIN 和 JWT 时如何记录身份验证失败的原因

        时间:2023-06-03
        <tfoot id='ucW1l'></tfoot>
        1. <i id='ucW1l'><tr id='ucW1l'><dt id='ucW1l'><q id='ucW1l'><span id='ucW1l'><b id='ucW1l'><form id='ucW1l'><ins id='ucW1l'></ins><ul id='ucW1l'></ul><sub id='ucW1l'></sub></form><legend id='ucW1l'></legend><bdo id='ucW1l'><pre id='ucW1l'><center id='ucW1l'></center></pre></bdo></b><th id='ucW1l'></th></span></q></dt></tr></i><div id='ucW1l'><tfoot id='ucW1l'></tfoot><dl id='ucW1l'><fieldset id='ucW1l'></fieldset></dl></div>

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

              <bdo id='ucW1l'></bdo><ul id='ucW1l'></ul>
            • <legend id='ucW1l'><style id='ucW1l'><dir id='ucW1l'><q id='ucW1l'></q></dir></style></legend>
                  <tbody id='ucW1l'></tbody>

                • 本文介绍了使用 OWIN 和 JWT 时如何记录身份验证失败的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 c# 自托管 OWIN 服务器,并已将我的应用程序配置为使用 JWT 授权,如下所示.这可以正常工作,无效令牌会被 401 Unauthorized 拒绝并接受有效令牌.

                  I am using a c# self hosted OWIN server and have configured my application to use authorise with JWT as below. This works properly, and invalid tokens are rejected with a 401 Unauthorized and valid tokens are accepted.

                  我的问题是我怎样才能写一个为什么请求被拒绝的日志.是不是过期了?是不是观众错了?没有令牌存在吗?我希望记录所有失败的请求,但我似乎找不到任何示例.

                  My question is how can I write a log of why requests are rejected. Was it expired? Was it the wrong audience? Was no token present? I want all failed requests to be logged, but I can't seem to find any example of how.

                  public class Startup
                      {
                          public void Configuration(IAppBuilder appBuilder)
                          {
                  
                              // Configure Web API for self-host. 
                              config.Routes.MapHttpRoute(
                                  name: "DefaultApi",
                                  routeTemplate: "api/{controller}/{id}",
                                  defaults: new { id = RouteParameter.Optional }
                              );
                  
                              // Enable 
                              config.Filters.Add(new AuthorizeAttribute());
                  
                              appBuilder.UseJwtBearerAuthentication(new JwtOptions());
                              appBuilder.UseWebApi(config);
                          }
                      }
                  

                  JwtOptions.cs

                  JwtOptions.cs

                  public class JwtOptions : JwtBearerAuthenticationOptions
                      {
                          public JwtOptions()
                          {
                              var issuer = WebConfigurationManager.AppSettings["CertificateIssuer"];
                              var audience = WebConfigurationManager.AppSettings["CertificateAudience"];
                  
                              var x590Certificate = Ap21X509Certificate.Get(WebConfigurationManager.AppSettings["CertificateThumbprint"]);
                  
                              AllowedAudiences = new[] { audience };
                              IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                              {
                                  new X509CertificateSecurityTokenProvider(issuer, new X509Certificate2(x590Certificate.RawData))
                              };
                          }
                      }
                  

                  我猜我需要实现自己的验证才能做到这一点,但也不确定如何实现.

                  I am guessing I will need to implement my own validation to do this, but not sure how to implement that either.

                  推荐答案

                  我知道现在已经很晚了,但是对于正在努力寻找答案的人来说很有用.

                  I know that it is quite late, but can be useful for one how is struggling to find an answer.

                  基本上 AuthenticationMiddleware 具有嵌入式日志记录.您只需要将 OWIN 日志重定向到您正在使用的记录器.NLog.Owin.Logging 适合我.log4net 也有类似的解决方案.

                  Basically AuthenticationMiddleware has embedded logging. You just need to redirect OWIN logs to logger you are using. NLog.Owin.Logging works well for me. There is similar solution for log4net.

                  有替代解决方案.扩展 JwtSecurityTokenHandler 并手动记录原因.

                  There is alternative solution. Extend JwtSecurityTokenHandler and log the reason manually.

                  public class LoggingJwtSecurityTokenHandler : JwtSecurityTokenHandler
                  {
                      public override ClaimsPrincipal ValidateToken(string securityToken, TokenValidationParameters validationParameters, out SecurityToken validatedToken)
                      {
                          try
                          {
                              return base.ValidateToken(securityToken, validationParameters, out validatedToken);
                          }
                          catch (Exception ex)
                          {
                              //log the error
                              throw;
                          }
                      }
                  }
                  

                  并像这样使用它:

                  app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
                  {
                      TokenHandler = new LoggingJwtSecurityTokenHandler()
                  });
                  

                  这篇关于使用 OWIN 和 JWT 时如何记录身份验证失败的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:自己发行 JWT 令牌与使用 IdentityServer4(OIDC) 进行 下一篇:验证 JWT 签名时出现 SecurityTokenSignatureKeyNotFoundE

                  相关文章

                  最新文章

                    <bdo id='9kKac'></bdo><ul id='9kKac'></ul>
                • <tfoot id='9kKac'></tfoot>

                  <small id='9kKac'></small><noframes id='9kKac'>

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