Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cookie cannot be read

I use asp.net cultures on my site to display the site in different languages, namely dutch(nl) and french(fr). The site has a button to change the site to another language by adding ?lang=nl to the url of the site. When you change the language a cookie is saved so the language stays for the next time you visit.

For some reason the site won't see the cookie. The cookie is there and is sent to the server, I checked with fiddler. Sometimes its value is null sometimes it is an empty string.

This is the code I use in my basepage class:

protected override void InitializeCulture()
{
    if (!Page.IsPostBack)
    {
        string cookieLang = "";

        try
        {
            cookieLang = HttpContext.Current.Request.Cookies["UserSettings"]["lang"];
        }
        catch (Exception e)
        {
            Response.Write(string.Format("<!-- {0} -->", e.Message));
        }

        if (!string.IsNullOrEmpty(Request.QueryString["lang"]))
        {
            cookieLang = Request.QueryString["lang"].ToLower();
        }

        if (!string.IsNullOrEmpty(cookieLang))
        {
            string selectedCulture = cookieLang == "fr" ? "fr-BE" : "nl-BE";

            this.Lang = cookieLang;

            UICulture = selectedCulture;
            Culture = selectedCulture;

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedCulture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedCulture);


            Response.Cookies["UserSettings"]["­lang"]= Lang;
            Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(7);
        }


        base.InitializeCulture();
    }
}

Can anyone tell me what I am doing wrong here?

Edit: I forgot to tell that this code works fine when I test it on my local pc, but it doesn't work when deployed on a server.

like image 963
Jerodev Avatar asked Apr 16 '26 02:04

Jerodev


1 Answers

I think your mistake is in how your handling cookies and in the sequence of events.

  • Step 1: Try getting a culture name from the cookie. If it exists, remember it.
  • Step 2: Try getting a culture name from the query string. If it exists, remember it.
  • Step 3: Set the current culture based on the previous.
  • Step 4: Save the culture name in the cookie.

The following code works for me consistently:

protected override void InitializeCulture()
{
    var cultureName = string.Empty;

    // Step 1
    var cookie = this.Request.Cookies["culture"];

    if (cookie != null)
    {
        cultureName = cookie.Value;
    }

    // Step 2
    var query = this.Request.QueryString["culture"];

    if (query != null)
    {
        cultureName = query;
    }

    // Step 3
    var culture = new CultureInfo(cultureName);

    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;

    // Step 4
    this.Response.Cookies["culture"].Value = culture.Name;

    base.InitializeCulture();
}
like image 161
Samu Lang Avatar answered Apr 18 '26 17:04

Samu Lang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!