How to handle Session on each request Asp.net mvc

Viewed 882

I have an web application running in mobile application(WebView).When user open the app, mobile app sends me the language info of the telephone through the request. Since the language value comes only on the first request and then deleted, I have to take it and store it for subsequent requests. I have tried PreRequestHandlerExecute and Application_AcquireRequestState on global.asax. I control the Session. if it is null I get the value from request, if it is not null I get it from session. But it seems like session is always null and always get the value from request. Any idea would be very helpful? Thanks for advance.

var dilKodu = Request.Headers.GetValues("languageCode");

        CultureInfo ci;
        if (dilKodu != null)
        {

            //I set culture from request if it is not null.
            ci = new CultureInfo(dilKodu[0].ToString());
            this.Session["Culture"] = ci;
        }
        else
        {
            if (HttpContext.Current.Session != null)
            {
                //I get culture from Session if its set before.
                ci = (CultureInfo)this.Session["Culture"];

                if (ci == null)
                {
                    string langName = "tr";
                    //If culture exist neither Session nor Request

                    ci = new CultureInfo(langName);
                    this.Session["Culture"] = ci;
                }
            }
            else
            {

                ci = new CultureInfo("tr");
            }

        }

        Thread.CurrentThread.CurrentUICulture = ci;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
3 Answers

Going by your code you are always going into the first if statement which will read and set the code form dilKodu

Your code should be more like this

if (HttpContext.Current.Session != null)
{
  ci = (CultureInfo) this.Session["Culture"];
  if (ci == null)
  {
    var langName = Request.Headers.GetValues("languageCode");
    Session["dilG"] = langName;
    ci = new CultureInfo(langName);
    Session["Culture"] = ci;
  }
}
else
{
  Session["dilG"] = "session hazır değil tr yapıldı";
  ci = new CultureInfo("tr");
}

Basically you have the if statements incorrectly setup for what you are trying to do.

Also it might be advisable to keep your session objects small, that is just store langName in the session and create a CultureInfo from that each time rather than store CultureInfo.

Is this an MVC web app? If so, you could try to save the value in the Controller.TempData property.

So instead of doing this.Session["culture"] = ci; you would add to the TempData property TempData.Add("culture", ci);. This would make the culture available for the next request. You would just need to make sure that you re-add the ci value in each request to make the value persist for as long as you need it.

Documentation for TempDataDictionary: https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.tempdatadictionary?view=aspnet-mvc-5.2

It was bcause of a setting in the web.config.

<httpCookies requireSSL="true" httpOnlyCookies="true"/>

I was trying to secure cookie but it prevent sessions. I delete it and now everything is works just fine. But I still need to find a way to secure cookies.

Related