【发布时间】:2019-01-10 18:05:25
【问题描述】:
我有一个 Angular 5 应用程序和一个 ASP.net core 2.0 rest api。当它们都在本地主机上运行时,我的身份验证工作得很好。我最近在另一台服务器上部署了其余的 api,突然我无法再进行身份验证。我通过以下方式在服务器上启用了 CORS:
app.UseCors(options =>
{
options.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials();
});
在 Angular 中,我设置了我的请求选项:
var headers: Headers = new Headers();
headers.append('Content-Type', 'application/json')
var requestOptions = new RequestOptions({
headers: headers,
withCredentials: true,
});
使用 fiddler,我查看我的登录 POST 请求并看到响应包含:
Set-Cookie: .AspNetCore.Identity.Application=CfD...;expires=Thu, 16 Aug 2018 21:25:02 GMT; path=/; samesite=lax; httponly
但是,在重定向回主页后,我发出一个 api 请求以查看用户是否已登录
var url = this.runtimeConfig.getConfig().apiUrl + `api/Auth/IsUserLoggedIn/`;
return this.http.get(url, requestOptions).toPromise().then(response => {
return <BoolDTO>ServerMessages.deserialize(response);
});
看看提琴手,这个 GET 请求不包含 COOKIE 标头,就像两个应用程序都在本地主机上时那样。
更奇怪的是,如果我随后将 URL http://<serverip>:<port>/api/Auth/IsUserLoggedIn/ 粘贴到浏览器的地址栏中,它会发送 COOKIE 标头并表示用户已登录。
我需要做些什么来让 Angular 的 http 客户端存储和重新发送 cookie?
【问题讨论】:
-
不确定这是否会帮助github.com/angular/angular/issues/24283,但它可能是 Content-Type 标头
-
登录请求(响应 Set-Cookie 标头)是否与后续 API 请求发送到相同的服务器:端口?您的问题表明 this.runtimeConfig.getConfig().apiUrl 可能与 http://
: 不同
标签: asp.net angular http session-cookies