我正在使用Microsoft.ApsNetCore.Cors 2.2
“ CORS策略已阻止从来源“ example.local”访问“ exampleapi.local”处的XMLHttpRequest:在请求的资源上不存在“ Access-Control-Allow-Origin”标头。”
我用这个设定设定
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
services.Configure<TokenSettings>(this.Configuration.GetSection("Tokens"));
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(opt =>
{
opt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Tokens:Issuer"],
ValidAudience = Configuration["Tokens:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["Tokens:SecurityKey"]))
};
});
services.AddMvc();
services.Configure<LdapConfig>(Configuration.GetSection("ldap"));
services.AddScoped<ILdapAuthenticationService, LdapAuthenticationService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IProjectService, ProjectService>();
services.AddScoped<IProjectMembersService, ProjectMembersService>();
services.AddScoped<IJourneyUsersService, JourneyUsersService>();
services.AddScoped<IProjectRolesService, ProjectRolesService>();
services.AddScoped<IPmoGuardianService, PmoGuardianService>();
services.AddScoped<IHolidaysService, HolidaysService>();
services.AddScoped<IMailService, MailService>(); …Run Code Online (Sandbox Code Playgroud)