简体   繁体   中英

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. Suppose a website www.xyz.com has four language version including English. 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. But it's not working. Please check the piece of code below to set cookie for English version.

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 (浏览器不一致)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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