我得到坏的请求错误,当我启动我的ASP,NET Core 3.1应用程序

7lrncoxx  于 2023-05-02  发布在  .NET
关注(0)|答案(1)|浏览(218)

当我启动ASP。NET Core项目,我收到了一个错误的请求。
我试着在IIS上运行我的应用程序,它工作了,但一些功能也导致了“错误请求”,我不知道原因。
网站正常工作,但现在我突然得到“错误请求”错误。
这是我的startup.cs文件

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddSession(option =>
        {
            option.IdleTimeout = TimeSpan.FromMinutes(5);

        });
       
    services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

    services.Configure<IdentityOptions>(option =>
        {
            option.Password.RequiredLength = 6;
            option.Password.RequireUppercase = false;
            option.Password.RequireNonAlphanumeric = false;
            option.Password.RequireLowercase = false;
        });

    services.AddMvc(config =>
        {
            var policy = new AuthorizationPolicyBuilder()
                            .RequireAuthenticatedUser()
                            .Build();
            config.Filters.Add(new AuthorizeFilter());
        });
    services.PostConfigure<CookieAuthenticationOptions>(IdentityConstants.ApplicationScheme,
        options =>
        {
            options.LoginPath = "/Accounting/Login";
            options.LogoutPath = "/Accounting/Logout";
            options.AccessDeniedPath = "/Accounting/AccessDenied";
            options.SlidingExpiration = true;
        });
    services.AddAuthorizationCore(
           options =>
           {
                //options.AddPolicy("UsersEdit",
                //    policy => policy.RequireAssertion(
                //              context =>context.User.HasClaim());
                // users policy
                options.AddPolicy("UsersShow",
                   policy => policy.RequireClaim("UsersShow", "true"));
               options.AddPolicy("UsersAdd",
                   policy => policy.RequireClaim("UsersAdd", "true"));
               options.AddPolicy("UsersEdit",
                   policy => policy.RequireClaim("UsersEdit", "true"));
               options.AddPolicy("UsersDelete",
                   policy => policy.RequireClaim("UsersDelete", "true"));
           });
    
    services.AddControllersWithViews();
    services.AddRazorPages();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseSession();
    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Products}/{action=Index}/{id?}");
            endpoints.MapControllerRoute(
                name: "areas",
                pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                );
           
            endpoints.MapRazorPages();
        });
    }
}

这是错误的屏幕截图:

klsxnrf1

klsxnrf11#

正如错误所说,头大小大于ASP中可接受的大小。NET Core应用程序。要适应更大的标头大小,可以增加允许的最大标头大小。
将此代码放在app之后。return();

app.Use(async (context, next) =>
{
    context.Request.Headers.Add("Max-Allowed-Headers-Size", "65536");
    await next.Invoke();
});

这里的数字65536意味着64 kb,除非您确切地知道为什么要这样做,否则不应该增加最大头大小。

相关问题