本地主机饼干未设置 [英] localhost cookies not set

查看:143
本文介绍了本地主机饼干未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置cookie中我的申请。

I'm trying to set a cookie in my application.

这里的code,它设置cookie:

Here's the code that sets the cookie:

public HttpResponseMessage LogIn(UserLoginVM user)
{
    // Do login stuff

    var cookie = new CookieHeaderValue("STUPID-COOKIE", "12345");
    cookie.Domain = Request.RequestUri.Host;
    cookie.Path = "/";
    cookie.HttpOnly = true;

    // Get user's profile

    HttpResponseMessage res = Request.CreateResponse<UserProfileVM>(HttpStatusCode.OK, profile);
    res.Headers.AddCookies(new CookieHeaderValue[] { cookie });

    return res;
}

从服务器的响应是:

The response from the server is the following:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
Set-Cookie: STUPID-COOKIE=12345; domain=localhost; path=/; httponly
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcUFJPSkVDVFNcU2Ftc2tpcC5TZXJ2aWNlV2ViTmV3XFNhbXNraXAuQXV0aEFQSVxTYW1za2lwLkF1dGhBUElcbG9naW4=?=
X-Powered-By: ASP.NET
Date: Wed, 18 Feb 2015 11:58:07 GMT
Content-Length: 8019

请注意下面的头:

Notice the following header:

Set-Cookie: STUPID-COOKIE=12345; domain=localhost; path=/; httponly

然而,当我在Chrome的资源选项卡下的去曲奇,没有设定。此外,当我发送一个请求到服务器没有cookie是在报头。

However, when I go under "Cookies" in "Resources" tab in Chrome, nothing is set. Also when I send a request to the server no cookie is in the headers.

这里的code,它会读取Cookie:

Here's the code that reads the cookie:

CookieHeaderValue cookie = Request.Headers.GetCookies("STUPID-COOKIE").FirstOrDefault();

饼干变量总是空。

我的应用程序在运行的http://本地主机:53998 和认证服务上的 HTTP://本地主机:60858

My application is running on http://localhost:53998 and the authentication service is running on http://localhost:60858

我的Chrome版本为40.0.2214.111。

My Chrome version is 40.0.2214.111.

下面是一个GIF演示问题:
http://i.imgur.com/q7lkXBz.gif

Here's a GIF demonstrating the problem: http://i.imgur.com/q7lkXBz.gif

编辑:这似乎非特异性的Chrome浏览器。这不会对火狐(V35)擦出火花。 GIF: http://i.imgur.com/ZewnEtc.gif

This seems to be non-specific to Chrome. This doesn't work on FireFox (v35) either. GIF: http://i.imgur.com/ZewnEtc.gif

推荐答案

我终于设法解决这个问题。

I finally managed to solve this.

在从API响应我不得不添加访问控制允许的凭据:真头,或通过添加在WebApiConfig类以下内容:

In the response from the API I had to add Access-Control-Allow-Credentials: true headers, or by adding the following in the WebApiConfig class:

public static void Register(HttpConfiguration config)
{
    var cors = new EnableCorsAttribute("*", "*", "*");
    cors.SupportsCredentials = true;

    config.EnableCors(cors);
}

然后我不得不启用它的客户端,由 XMLHTT prequest withCredentials 属性$ C>对象真正。在AngularJS应用程序的配置功能,您可以执行以下操作:

Then I had to enable it on the client-side, by setting the withCredentials property in the XMLHTTPRequest object to true. In AngularJS app config function you can do the following:

$httpProvider.defaults.withCredentials = true;

此外,Chrome浏览器,我不得不省略(或将其设置为空)。

希望这有助于其他人使用此苦苦挣扎。

Hope this helps others struggling with this.

这篇关于本地主机饼干未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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