使用 Blazor 0.9.0 和 ASP.NET Core 3 预览版 4 进行 JWT 身份验证

我遵循了本教程:https ://medium.com/@st.mas29/microsoft-blazor-web-api-with-jwt-authentication-part-1-f33a44abab9d (适用于 .NET core 2.2)。


这是我的启动类


    public class Startup

    {

        // This method gets called by the runtime. Use this method to add services to the container.

        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940

        public IConfiguration Configuration { get; }

        public Startup (IConfiguration configuration)

        {

            Configuration = configuration;

        }


        public void ConfigureServices(IServiceCollection services)

        {

            services.AddMvc().AddNewtonsoftJson();

            //services.AddMvcCore().AddAuthorization().AddNewtonsoftJson();


            services.AddAuthentication(options =>

            {

                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;

                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

            })

            .AddJwtBearer(options =>

            {

                options.TokenValidationParameters = new TokenValidationParameters

                {

                    ValidateIssuer = true,

                    ValidateAudience = true,

                    ValidateLifetime = true,

                    ValidateIssuerSigningKey = true,

                    ValidIssuer = Configuration["Jwt:Issuer"],

                    ValidAudience = Configuration["Jwt:Audience"],

                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))

                };

            });


            services.AddResponseCompression(opts =>

            {

                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(

                    new[] { "application/octet-stream" });

            });


        }

我还在 Api 控制器 SampleDataController 上添加了 [Authorize]。


我预计(根据帖子)在访问数据时会出现 401(未经授权)错误,但我收到有关缺少授权中间件的投诉

http://img4.mukewang.com/63a6a1bb0001bbe006540350.jpg

jeck猫
浏览 141回答 2
2回答

吃鸡游戏

将两者 app.UseAuthentication()和app.UseAuthorization() 之后 app.UseRouting()app.UseRouting();app.UseAuthentication();app.UseAuthorization();app.UseEndpoints(routes =>     {         routes.MapDefaultControllerRoute();     });

冉冉说

如果您发送带有授权令牌的请求,并且Startup.cs文件中未设置服务器授权,则 API 将返回错误消息<Called method> contains authorization metadata, but a middleware was not found that supports authorization...解决方法是在Startup.cs文件中添加以下行,BETWEENapp.UseRouting()和app.UseEndpoints(...):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app.UseRouting();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //AUTHORIZING&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app.UseAuthentication();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app.UseAuthorization();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app.UseEndpoints(endpoints =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endpoints.MapRazorPages();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endpoints.MapControllers();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endpoints.MapFallbackToFile("index.html");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP