【问题标题】:How can I determine why a bear token failed如何确定熊令牌失败的原因
【发布时间】:2020-08-28 11:45:22
【问题描述】:

运行一个基本的 ASP.NET Core RESTful 服务器,我使用我在端点上提供的 JWT 令牌来保护我的端点(这是目前的概念证明)。当我使用 Postman 进行测试时,我能够正确地进行身份验证,但是来自控制台应用程序,得到 401,未授权。

这是我在 ServiceExtensions 类中的内容:

public static IServiceCollection ConfigureJwtAuthentication(this IServiceCollection services,
            IConfiguration config)
        {
            var section = config.GetSection("Jwt");
            var jwtOptions = section.Get<JwtConfigOptions>();

            services.AddAuthentication(options =>
                {
                    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

                })
                .AddJwtBearer(options =>
                {
                    //options.Authority = jwtOptions.AuthorityUrl;
                    options.RequireHttpsMetadata = false;
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,

                        ValidIssuer = jwtOptions.Issuer,
                        ValidAudience = jwtOptions.Audience,
                        IssuerSigningKey = jwtOptions.SymmetricSecurityKey
                    };
                });

            return services;
        }

这是我的 JwtConfigOptions 类:

public class JwtConfigOptions
    {
        public string Key { get; set; }
        public string Issuer { get; set; }
        public string Audience { get; set; }
        public string AuthorityUrl { get; set; }
        public string AudienceUrl { get; set; }
        public SymmetricSecurityKey SymmetricSecurityKey => new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Key));
    }

并且在 appsettings.json 中有值,所以我使用与审查令牌相同的值来创建。

有没有办法记录给定令牌被拒绝的原因?

【问题讨论】:

    标签: asp.net-core debugging jwt authorize


    【解决方案1】:

    我找到了答案。我发现 this site 链接到一个 github 解决方案,该解决方案包含 Microsoft.AspNetCore.Authentication.JwtBearer 程序集中的项目的源代码。我附加到 JwtBearerHandler 项目并能够单步执行代码。原来我在标头中错误地编码了不记名令牌。实际上有正确的代码在 /redface 之前注释掉了这一行

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 1970-01-01
      • 2020-12-02
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 2021-08-15
      相关资源
      最近更新 更多