简体   繁体   English

如何在 ASP.NET C# MVC 中为特定域设置 cookie

[英]How to set cookie for a specific domain in ASP.NET C# MVC

I want to set cookies for a specific domain to fetch data in English version in C# while crawling the website.我想为特定域设置 cookie,以便在抓取网站时在 C# 中获取英文版本的数据。 Suppose a website www.xyz.com has four language version including English.假设一个网站www.xyz.com有四种语言版本,包括英语。 The actual data in database are in any other language (suppose Swedish).数据库中的实际数据是任何其他语言(假设瑞典语)。 While crawling the site to get all of its links and text, need to fetch the contents of English version.在抓取网站以获取其所有链接和文本时,需要获取英文版本的内容。 That's why I want to set cookies there.这就是我想在那里设置 cookie 的原因。 But it's not working.但它不起作用。 Please check the piece of code below to set cookie for English version.请检查下面的一段代码来设置英文版本的cookie。

if (_domain == "www.xyz.com")
{
    HttpCookie CreateLangCookie()
    {
        HttpCookie cookie = new HttpCookie("Cookies");
        cookie.Name = "lang";
        cookie.Value = "en";
        cookie.Domain = ".xyz.com";
        cookie.Path = "/";
        cookie.Expires = DateTime.MaxValue;
        return cookie;
    }
    Response.Cookies.Add(CreateLangCookie());
}

您可能想要扩展您的问题以注意您所说的“不工作”是什么意思并包括您使用的浏览器,但解决方案可能是不使用显式 cookie,如本答案所述: Cookies with and without the Domain Specified (浏览器不一致)

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

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