在Angular 9+;中启用Windows身份验证后,飞行前(选项)请求失败;Asp.net核心Web API
我们有asp.net核心web api和angular 9应用程序。Asp.net核心web API使用以下代码启用了windows身份验证。此外,为了进行测试,我正在为所有人启用CORS在Angular 9+;中启用Windows身份验证后,飞行前(选项)请求失败;Asp.net核心Web API,angular,webapi,Angular,Webapi,我们有asp.net核心web api和angular 9应用程序。Asp.net核心web API使用以下代码启用了windows身份验证。此外,为了进行测试,我正在为所有人启用CORS "iisSettings": { "windowsAuthentication": true, "anonymousAuthentication": false, "iisExpress": {
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:44305",
"sslPort": 0
}
},
services.AddCors(options =>
{
options.AddPolicy("CustomCorsPolicy",
builder => builder.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
app.UseCors("CustomCorsPolicy");
当我发出GET请求时,我得到的是响应,但当发出POST请求时,它失败了。我已经将crendtial设置为true。下面是我设置的拦截器
intercept(httpreq: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
httpreq= httpreq.clone({
withCredentials: true
});
return next.handle(httpreq);
}
访问位于“”的XMLHttpRequesthttps://localhost:44305/anotherdata/test“起源”http://localhost:4200'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'access control Allow Origin'标头。我使用的是.NET Core 3.1,这就是我的“API”的方式这个项目看起来不错。这就是让CORS进入working Startup.cs所需的全部内容**需要重新启动IIS express服务器以获取更改
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors(builder =>
builder.WithOrigins("http://localhost:21460").AllowCredentials());
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
我正在使用.NETCore3.1,这就是我的“API”项目的外观。这就是让CORS进入working Startup.cs所需的全部内容**需要重新启动IIS express服务器以获取更改
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors(builder =>
builder.WithOrigins("http://localhost:21460").AllowCredentials());
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
您提供的代码看起来不错。您是否已将拦截器连接到提供程序中的模块中,例如在app.module.ts中。类似于从'@angular/common/HTTP'导入{HttpClientModule,HTTP_INTERCEPTORS};和提供者:[//Http拦截器(s)-使用客户端凭据添加[{提供:Http_拦截器,useClass:HttpRequestInterceptor,multi:true}],是的,我已经在应用程序模块上注册了它。一个想法。如果你的拦截器在或中,那么很明显,请道歉。我相信你已经做到了。locahost:8000是API,localhost:4200是Angular应用程序。我在localhost:4200上安装了拦截器。您提供的代码看起来不错。您是否已将拦截器连接到提供程序中的模块中,例如在app.module.ts中。类似于从'@angular/common/HTTP'导入{HttpClientModule,HTTP_INTERCEPTORS};和提供者:[//Http拦截器(s)-使用客户端凭据添加[{提供:Http_拦截器,useClass:HttpRequestInterceptor,multi:true}],是的,我已经在应用程序模块上注册了它。一个想法。如果你的拦截器在或中,那么很明显,请道歉。我相信你已经做到了。locahost:8000是API,localhost:4200是Angular应用程序。我在本地主机上有拦截器:4200