Is it possible to get the name and value of all sessions by javascript?

1

I'm creating several sessions, and would like to get the name and value of all of them through JavaScript. Home When creating two Sessions in Asp.Net MVC, for example:

Session["usuario"] = "user";
Session["tipo"] = "comum";

I'd like to get the two sessions on View, without knowing the name. Example:

<script>
  var sessions = getTodasAsSessions(); // Existe alguma função desse tipo?
  for(var i = 0; i < sessions.length; i++){
      alert("Nome da Session: " + sessions[i].Name + " Valor: " + sessions[i].Value);
  }
</script>

Is this possible?

    
asked by anonymous 24.06.2015 / 23:31

1 answer

1

You can not access a server-side session variable on the client side.

Now you can "play" a lot with your code to get the values of the sessions on the client side. I'll give you a little idea of what you can do:

  • Create a hidden field in HTML and assign the session value to the hidden field on the server side.

    hfSessionValue.Value = HttpContext.Current.Session("NomeDaSession").ToString

  • Create JavaScript function to fetch the hidden field value: function getSessionValue() { return documento.getElementById("hfSessionValue").value; }

  • 25.06.2015 / 10:37