简体   繁体   中英

JWT Bearer Keeps returning 401 Status - Bearer error="invalid_token", error_description="The signature is invalid"

I implemented Jwt Bearer in my Web API, I successfully get a login token. I copy/paste this token to use it for authorization and I receive

Bearer error="invalid_token", error_description="The signature is invalid"

ValidIssuer is the WebAPI Url, deployed in Azure

ValidAudience is the Angular Front-End URL, also deployed in Azure

I spent hours looking for the problem and I don't seem to find a solution.

Even turning ValidateIssuer & -Audience to false, doesn't solve the problem. Did I miss something or do you see the error?

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder =>
                {
                    builder.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod();
                });
            });
            services.ConfigureIISIntegration();

            services.ConfigureMsSqlContext(Configuration);

            services.ConfigureRepositoryWrapper();

            services.AddAuthentication(opt => {
                opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                opt.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.SaveToken = true;
                options.RequireHttpsMetadata = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidIssuer = Configuration["JWT:Issuer"],
                    ValidAudience = Configuration["JWT:Audience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Secret"]))
                };
            });

            services.AddControllers();
        }

        
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseDeveloperExceptionPage();

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseCors("CorsPolicy");

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.All
            });

            app.UseAuthentication();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

Solution to my problem, maybe small but impactfull detail

I forgot to put Authorize-attribute on top of my Request method

[HttpGet, Authorize]
public IActionResult GetMaterialDetails()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM