如何解决“未指定身份验证方案,并且未找到 DefaultChallengeScheme”的问题

GPS*_*ice 4 c# asp.net-mvc .net-core visual-studio-code asp.net-core

[![消息错误]] [1]: https://i.stack.imgur.com/ciKCK.png

我的观点是,如果有人在没有登录的情况下进入授权页面,它将被重定向到登录页面。

这是我的代码(.NET Core 3.1):

我的控制器

    [Authorize]
    public IActionResult Username()
    {
        var lstUsername = _dbContext.MT_USERNAME.ToList();
        return View(lstUsername);
    }
Run Code Online (Sandbox Code Playgroud)

我的 Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<GPServiceDbContext>(options =>
        {
            options.UseSqlServer(xxx);
        });
        services.AddControllersWithViews();

        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromMinutes(1);   
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthentication();
        app.UseStatusCodePages(context =>
        {
            var response = context.HttpContext.Response;
            if (response.StatusCode == (int)HttpStatusCode.Unauthorized ||
                response.StatusCode == (int)HttpStatusCode.Forbidden)
                response.Redirect("/Login/Login");
            return Task.CompletedTask;
        });
        app.UseAuthorization();
        app.UseSession();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Login}/{action=Login}/{id?}");
        });
    }
Run Code Online (Sandbox Code Playgroud)

Ste*_*bor 6

就我而言,身份验证选项参数上只有一个简单的错误

options.DefaultAuthenticateScheme
Run Code Online (Sandbox Code Playgroud)

而不是这个:

options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
Run Code Online (Sandbox Code Playgroud)