【问题标题】:ASP.NET Web API JWT intermittent 401 errorsASP.NET Web API JWT 间歇性 401 错误
【发布时间】:2019-09-07 02:48:14
【问题描述】:

在使用有效令牌的 Web API 中使用 JwtBearerAuthentication 时,我偶尔会遇到 401 错误。它似乎在大多数情况下都可以正常工作,但偶尔它会无缘无故地开始 401。我可以在邮递员给我错误后立即使用具有完全相同标记的邮递员并且它工作正常。

似乎一旦它开始提供 401,它们就会越来越频繁地发生,直到应用程序无法使用。然后我回收应用程序池,它似乎清理了一段时间。内存使用似乎并没有失控,所以我认为不是内存泄漏。

这是启动时的配置


    var configurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(
        authority + "/.well-known/openid-configuration",
        new OpenIdConnectConfigurationRetriever(),
        new HttpDocumentRetriever());

    app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
    {
        AuthenticationMode = AuthenticationMode.Active,
        TokenValidationParameters = new TokenValidationParameters
        {
            ValidAudience = "AUDIENCE",
            ValidIssuer = authority,
            IssuerSigningKeyResolver = (token, securityToken, identifier, parameters) =>
            {
                var discoveryDocument = Task.Run(() => configurationManager.GetConfigurationAsync()).GetAwaiter().GetResult();
                return discoveryDocument.SigningKeys;
            }
        }
    });

这里是相关的包


    <packages> 
      <package id="Microsoft.IdentityModel.JsonWebTokens" version="5.3.0" targetFramework="net462" /> 
      <package id="Microsoft.IdentityModel.Logging" version="5.3.0" targetFramework="net462" /> 
      <package id="Microsoft.IdentityModel.Protocols" version="5.3.0" targetFramework="net462" /> 
      <package id="Microsoft.IdentityModel.Protocols.OpenIdConnect" version="5.3.0" targetFramework="net462" /> 
      <package id="Microsoft.IdentityModel.Tokens" version="5.3.0" targetFramework="net462" /> 
      <package id="Microsoft.Owin" version="4.0.0" targetFramework="net462" /> 
      <package id="Microsoft.Owin.Host.SystemWeb" version="4.0.0" targetFramework="net462" /> 
      <package id="Microsoft.Owin.Security" version="4.0.0" targetFramework="net462" /> 
      <package id="Microsoft.Owin.Security.Jwt" version="4.0.0" targetFramework="net462" /> 
      <package id="Microsoft.Owin.Security.OAuth" version="4.0.0" targetFramework="net462" /> 
      <package id="System.IdentityModel.Tokens.Jwt" version="5.3.0" targetFramework="net462" /> 
    </packages>

我使用这个 tutorial 构建了应用程序

有人见过这样的事情吗?

【问题讨论】:

    标签: c# asp.net asp.net-web-api jwt


    【解决方案1】:

    很少有信息可以找到可能的原因。但是,我们可以考虑一件事,委托本身是一个同步块,但是您正在调用 async 方法和同一上下文中的等待者。这很可能会被死锁阻塞。

    var discoveryDocument = Task.Run(() => configurationManager.GetConfigurationAsync()).GetAwaiter().GetResult();
    

    而是将代码转换为这个 -

    var discoveryDocument = configurationManager.GetConfigurationAsync().ConfigureAwait(false).GetAwaiter().GetResult();
    

    您可以在这篇博文中了解它 - https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

    不确定这是否是导致问题的原因。一个可能的假设可能是这段代码导致了死锁,并且您可能用完了池中的线程,并且作为副作用401 随着时间的推移而增加。

    我建议运行内存分析器,看看这是否真的导致了死锁。

    如果这些解决了问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 1970-01-01
      • 2013-11-18
      • 2012-06-10
      • 2016-08-30
      • 2021-12-24
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多