.net - .NET CORE 2.1 中的 CORS - 简单配置不返回标头
问题描述
我基本上遵循了这个文档:https ://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.1试图在我的项目中正确设置它,但到目前为止没有缺乏。我想我尝试了每一种组合,包括中间人和服务,改变订单等。
在使用邮递员进行测试时,在对我的 API 端点进行 POST 调用时,我看不到任何与 CORS 相关的标头。
我正在使用 API 并在其上运行的应用程序http:/localhost:3000
在尝试对 API 端点进行 POST 调用时出现 CORS 错误。
这是我的整个Startup.cs
班级:
public class Startup
{
public Startup(IHostingEnvironment hostingEnvironment, IConfiguration configuration)
{
HostingEnvironment = hostingEnvironment;
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public IHostingEnvironment HostingEnvironment { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddInjectionByAttribute();
services.AddDbContext<MoneyTrackigContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<UserContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UserDbConnection")));
services.AddIdentity<IdentityUser, IdentityRole>(o =>
{
o.Password.RequireDigit = false;
o.Password.RequiredLength = 6;
o.Password.RequireLowercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<UserContext>()
.AddDefaultTokenProviders();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // => remove default claims
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = HostingEnvironment.IsDevelopment();
cfg.SaveToken = true;
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = Configuration.GetSection("JwtOptions")["JwtIssuer"],
ValidAudience = Configuration.GetSection("JwtOptions")["JwtIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("JwtOptions")["JwtKey"])),
ClockSkew = TimeSpan.Zero // remove delay of token when expire
};
});
services.AddAuthorization(o =>
{
o.AddPolicy(Policy.DefaultUser, policy => policy.RequireClaim(ClaimName.User));
});
services.AddAutoMapper();
services.AddCors();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseCors(b => b.WithOrigins("http:/localhost:3000").AllowAnyHeader().AllowAnyMethod());
app.UseMvc();
}
}
我错过了什么明显的东西吗?任何可能导致此问题的想法。整个事情看起来很简单,但被卡住了。
解决方案
推荐阅读
- forms - 表单中的条件字段
- azure - Azure VM 处于联机状态,但该 VM 的 Azure devops 代理处于脱机状态
- angular - 问题更改根并且在角度之后不更改页面
- javascript - 如何解决“无法解决:App.vue 中的组件”
- flutter - 打印功能在 Flutter Stateless Widget 中不起作用
- parent-child - 我可以用 LineNbr 值填充字段 artran.OrigInvoiceLineNbr 吗?
- r - 是否可以从具有线和点几何数据的 ggplot 的图例中删除绘制为点几何的组?
- jquery - 为什么可以为 svg fill 分配一个数组作为其值?在这种情况下,数组会做什么?
- c# - 如何将命令行参数传递给 Windows 服务的 MSI 文件
- python - 与不匹配的类数和 target_names 相关的错误