【发布时间】:2018-10-09 02:56:43
【问题描述】:
对于基于 cookie 的身份验证,我的服务器将 Set-Cookie 发送到我的 Angular 应用程序。但是,应用程序不会在进一步的请求中发回该值。以下是我的代码。
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true //this is required so that Angular returns the Cookies received from the server. The server sends cookies in Set-Cookie header. Without this, Angular will ignore the Set-Cookie header
};
public getUserProfile(){
console.log('contacting server at '+this.API_URL +this.GET_USER_PROFILE_URL+"with httpOptions "+httpOptions);
return this.http.get(this.GET_USER_PROFILE_URL,httpOptions )
.map(response=>{
console.log('response from backend service',response);
let result= <ServerResponse>response;
console.log("result is "+result.result+' with additional information '+result.additionalInformation)
return result;
})
.catch(this.handleError);
}
服务器在我的代码的200OK中发送cookie如下(这里没有显示)
Set-Cookie: id=...
但是,下一条消息在 cookie 中没有 id,因此服务器返回 401。如果我使用浏览器的调试工具手动添加 Cookie,则得到 200OK。因此,我确定是 cookie 中缺少 id 值导致了问题。
我做错了什么?我是否需要明确存储在Set-Cookie 中收到的 cookie 并在进一步的请求中明确添加它?
更新 -
在最初加载 SPA 时,服务器会发送 Set-Cookie 标头以及与 CSRF 相关的一些其他 cookie 信息。我注意到该 cookie 仍然由应用程序发送。会不会是 Angular 尊重第一个 Set-Cookie 标头而忽略了后面的标头?
我添加了几张图片来解释我的意思
在签名期间,客户端发送一个与 CSRF 相关的 cookie。我不认为这是必需的,因为客户端也发送 CSRF Header 但出于某种原因它确实如此。服务器使用带有 id 的 Set-Cookie 进行响应
然后当我要求配置文件时,客户端再次发送 CSRF cookie 但不发送 id cookie
【问题讨论】:
标签: angular