C#WebApi + Angular 4 Set-Cookie不保存Cookie [英] C# WebApi + Angular 4 Set-Cookie doesn't save cookies

查看:153
本文介绍了C#WebApi + Angular 4 Set-Cookie不保存Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向我的API发出一个http请求,该请求应该可以通过Set-Cookie标头向我发送两个cookie。

I'm doing an http request to my API that is supposed to send me back two cookies with the Set-Cookie Header.

似乎cookie是正确发送此Init请求,但它们未保存在Chrome中,因此对于以后的请求,不会发送cookie。

It seems that the cookie is correctly send back for this Init Request but they are not saved in Chrome, so for later requests the cookies are not sent.

我的Init响应标头是:

My Init response headers are :

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:content-type
Access-Control-Allow-Origin:http://localhost:4200
Cache-Control:no-cache
Content-Length:0
Date:Thu, 20 Apr 2017 09:25:45 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
Set-Cookie:ASP.NET_SessionId=vpt0f3m4jp2gwghm1mmrxyhm; path=/; HttpOnly
Set-Cookie:ARRAffinity=ccebcfbd458447f3ab209749a770ad1f93d4b4c907a7cac9ef717d5fbf543fe3;Path=/;Domain=xxx
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

但是Chrome的Cookie存储中没有存储任何内容。

But nothing is stored in the cookies storage of Chrome.

是否来自

Expires:-1

预先感谢!

推荐答案

我想您的C#后端运行在与Angular应用程序不同的端口。 Cookie不在同一主机的不同端口之间共享。 Chrome浏览器显示对当前选项卡URL(即Angular应用程序)有效的cookie。因此,您在此处看不到后端Cookie。

I guess your C# backend runs at a different port than your Angular application. Cookies are not shared among different ports of the same host. Chrome displays cookies valid for the current tab URL, which is the Angular application. So you cannot see your backend cookies there.

您可以检查从Angular应用程序到C#后端的XHR请求的标头,它们应包含指定的cookie(ASP.NET_SessionId,ARRAffinity)。

You can check your headers of XHR requests from the Angular application to the C# backend, they should contain the specified cookies (ASP.NET_SessionId, ARRAffinity).

这篇关于C#WebApi + Angular 4 Set-Cookie不保存Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆