c# - .NET Core 3.1 Cors 问题
问题描述
我已遵循 Microsoft 文章中列出的指南:在 ASP.NET Core 中启用跨域请求 (CORS),但我仍然无法从本地 vue 网站或 PostMan 访问 API。有什么建议么?
这是 AllowedHosts 中定义的内容:
"AllowedHosts": "http://localhost;http://localhost:8080"
这是启动类:
using App.Core.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace App.Core
{
public class Startup
{
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.AddDbContext<ImportContext>((builder) =>
{
builder.UseSqlServer(Configuration.GetConnectionString("ParsingAppDb"));
});
services.AddDbContext<CodeAdminContext>((builder) =>
{
builder.UseSqlServer(Configuration.GetConnectionString("ParsingAppDb"));
});
services.AddScoped(typeof(IImportContext), typeof(ImportContext));
services.AddScoped(typeof(ICodeAdminContext), typeof(CodeAdminContext));
services.AddTransient(typeof(Logic.IImporter), typeof(Logic.Importer));
services.AddTransient(typeof(Logic.I2964Procssor), typeof(Logic.Processor_2964));
services.AddTransient(typeof(Logic.I2965Procssor), typeof(Logic.Processor_2965));
var allowedHosts = Configuration.GetValue(typeof(string), "AllowedHosts") as string;
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
if (allowedHosts == null || allowedHosts == "*")
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
return;
}
string[] hosts;
if (allowedHosts.Contains(';'))
hosts = allowedHosts.Split(';');
else
{
hosts = new string[1];
hosts[0] = allowedHosts;
}
builder.WithOrigins(hosts)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
}); services.AddControllers();
}
// 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.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
即使使用提琴手也没有任何帮助,只是被拒绝的电话。相反,如果我将它设置为任何来源,我可以让 PostMan 工作,但 vue 网站然后返回没有设置任何 Accept-Control。
更新:我已经安装了 Microsoft.AspNetCore.Cors 包。
解决方案
我发现了问题。它是配置值:
"AllowedHosts": "http://localhost;http://localhost:8080"
显然,此值以另一种方式使用,创建一个单独的 Cors 部分并将允许的 cors 主机放置在那里,然后将 AllowedHosts 值更改回 * 修复了该问题。
推荐阅读
- python - 从外部服务器传递 webhook 时,Watson Assistant 给出错误 [任何地方都没有答案]
- python - 通过 Regex 和 Maxsplit 拆分字符串会返回多个拆分
- java - 如何使用 Spring Data JPA 正确外部化本机 SQL 查询
- java - 更改列表中的单元格的功能
- c++ - myComboBox->Dismiss(); 在 wxEVT_TEXT 和 wxEVT_COMBOBOX_DROPDOWN 下不工作
- php - Laravel 8 用户表列名
- r - 用于 R 中 misc3d 的数据帧到数组
- sml - 错误 SML:错误:未绑定的变量或构造函数:valof
- android - :app:generateCrashlyticsSymbolFileRelease 错误:generateCrashlyticsSymbolFileRelease 所需的未剥离本机库路径不存在:
- kotlin - 检查其中一个值是否包含换行符