简体   繁体   English

使用IIS Express在域和子域之间共享cookie

[英]Sharing cookies between domain and a subdomain using IIS express

I'm trying to share a cookie between two ASP.NET mvc 6 apps : 我正在尝试在两个ASP.NET MVC 6应用程序之间共享Cookie:

  • The first one is a web api app and it should represent the main domain "localhost". 第一个是网络api应用,它应代表主域“ localhost”。

  • Second one is an empty mvc website client app, with one index.html that calls the web api via simple ajax. 第二个是一个空的mvc网站客户端应用程序,其中一个index.html通过简单的ajax调用Web api。 this app represents the subdomain "cleint.lohalhost". 该应用程序代表子域“ cleint.lohalhost”。

For somereason, my browser is unable to set the cookie on the client app. 出于某种原因,我的浏览器无法在客户端应用程序上设置Cookie。 Details below : 详情如下:

-Set cookie header generated by web api -设置Web API生成的Cookie标头

Set-Cookie:mytoken=mytokenvalue; domain=.localhost; path=/; samesite=lax

-Ajax call : -Ajax通话:

$.get({
    url: 'http://localhost:5004/api/values',
    success: function (response) { console.log(response); },
    error: function (error) { console.log(error); },
    xhrFields: {
        withCredentials: true
    },
    crossDomain: true
});

-And finally, the code that sets the cookie on the web api app : -最后,是在网络api应用上设置cookie的代码:

[HttpGet]
public IEnumerable<string> Get()
{
    Response.Cookies.Append("mytoken", "mytokenvalue", new CookieOptions() { Domain = ".localhost" });
    return new string[] { "value1", "value2" };
}

-I use chrome browser if it's relevant. -如果相关,我会使用chrome浏览器。

I appreciate any kind of help, thank you. 感谢您的帮助,谢谢。

It turned out my browser was still hungry for another '.' 原来,我的浏览器仍然渴望另一个'。'。 on the domain name, so I ended up replacing 'localhost' (on both domain names) with 'myapp.dev' and it worked. 在域名上,所以我最终用两个“ myapp.dev”替换了“ localhost”(两个域名)。 It is also important to note that from the api side, I had to send the domain name cookie option with the value '.myapp.dev' instead of 'client.myapp.dev' 同样重要的是要注意,从api端,我必须发送值为“ .myapp.dev”而不是“ client.myapp.dev”的域名cookie选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM