zoukankan      html  css  js  c++  java
  • .net core 简单集成JWT报No authenticationScheme was specified, and there was no DefaultChallengeScheme found错误

               #region JWT 认证
                services
                  .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                  //.AddCustomAuth(o => { })
                  .AddJwtBearer(options => {
                      options.TokenValidationParameters = new TokenValidationParameters {
                          ValidIssuer = Configuration["JwtSetting:Issuer"],
                          ValidAudience = Configuration["JwtSetting:Audience"],
                          IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtSetting:SecurityKey"])),
                          // 默认允许 300s  的时间偏移量,设置为0
                          ClockSkew = TimeSpan.Zero
                      };
                  });
                #endregion
                #region MVC修改控制器描述
                services.AddHttpContextAccessor();
                services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
                services.AddMvc(config => {
                    config.RespectBrowserAcceptHeader = true;
                    //注入MVC拦截器
                    config.Filters.Add<ApiFilter>();
                })
                .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new ContractResolver())
                .AddJsonOptions(options => options.SerializerSettings.Converters.Add(new ChinaDateTimeConverter()))
                .AddFormatterMappings(options => options.SetMediaTypeMappingForFormat("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .AddFormatterMappings(options => options.SetMediaTypeMappingForFormat("jpeg", "image/jpeg"))
                .AddFormatterMappings(options => options.SetMediaTypeMappingForFormat("jpg", "image/jpeg"));
           #endregion

    在ConfigureServices中  注册JWT必须在注册MVC之前 否则就会报No authenticationScheme was specified, and there was no DefaultChallengeScheme found错误

                app.UseAuthentication();
                app.UseMvc();

    在Configure中  添加JWT验证也必须在MVC之前,否则也会报错。

  • 相关阅读:
    mysql报Fatal error encountered during command execution的解决办法
    C语言之算法初步(汉诺塔--递归算法)
    C语言中变量的作用域和生命周期
    C语言数据在内存分配
    ~~~
    数据结构笔记
    SQL笔记
    Java零碎知识点
    如何让eclipse在程序修改后,点击运行可以自动保存。
    [转载] java中静态代码块的用法 static用法详解
  • 原文地址:https://www.cnblogs.com/wsprince/p/11649421.html
Copyright © 2011-2022 走看看