angular - 不能同时使用 HttpHeaders 和 WithCredentials 选项
问题描述
我有一个 .net5 WebApi,它需要从我的 Angular 应用程序发送凭据。当我使用 HttpInterceptor 指定我的所有请求将在每次调用中包含凭据时,一切都很好,但是如果我将withCredentials
选项与任何HtpHeaders
请求放在一起,则不会通过 CORS 验证。
这是我的拦截器的样子(工作正常):
@Injectable({ providedIn: 'root' })
export class HttpInterceptService implements HttpInterceptor {
constructor() { }
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request.clone({
withCredentials: true
}));
}
}
但如果调用如下,CORS 验证不会通过:
public apiCall(): Observable<any> {
return this.httpClient.get(API_URL, {
withCredentials: true,
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'my-auth-token'
})
});
}
CORS 错误(当我将 withCredentials 与标头一起使用时):
但是,如果我删除标题,一切正常:
public apiCall(): Observable<any> {
return this.httpClient.get(API_URL, {
withCredentials: true
});
}
解决方案
推荐阅读
- python - 合并数据框并删除重复的列
- javascript - 使用 Node.js 使用 azure 媒体服务(视频编码器)从视频生成缩略图
- arrays - 我想解构我的 api 并使用 react 返回它如何在我的用例中实现这一点?
- ssl - 托管没有 ssl 证书的 Blazor Web 应用程序
- python - 理解python中的嵌套函数
- flutter - 徽章结束并隐藏文字
- c# - 在c#中检查用户ID是否存在于Discord服务器中
- reactjs - 平面列表导航反应原生
- php - 在 laravel 7x 上找不到特征“Illuminate\Foundation\Auth\AuthenticatesUsers”
- flutter - Listview 和 Checkbox 使用 StatefulWidget(setState)