using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
public void ConfigureServices(IServiceCollection services)
{
// 添加CSP策略到服務(wù)容器,以便在中間件中使用
services.AddSingleton<CspOptions>(options =>
{
options.DefaultSources = "self";
options.ScriptSources = "self https://apis.google.com";
// 可以添加更多源和其他CSP指令...
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// 添加CSP中間件
app.UseCsp(options =>
{
options.DefaultSources(directives => directives.Self());
options.ScriptSources(sources => sources.Self().CustomSources("https://apis.google.com"));
// 配置其他CSP指令...
});
// 其他中間件和路由配置...
}