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

Asked

Viewed 1,351 times

1

I am creating several Séssions, and would like to get the name and value of all by Javascript.
When creating two Sessions in Asp.Net MVC, for example:

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

I’d like to get both Sesssions on View, without knowing their 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>

That’s possible?

  • 1

    I believe that not because it would open a security breach, but what is the purpose of it?

  • You want to pick up the front-end sessions?

  • Exact Guilhermenascimento.

  • Maiconcarraro, I am doing a project where depending on the type of user I create several different Sesssions, and I have to recover them in the View. It really makes sense the issue of security, but cryptography would not solve?

  • Given that you were able to implement the getTodasAsSessions(), What are you going to do with it? You’re going to use this to selectively render things on the front end, to advance the permission errors, ...?

  • I haven’t implemented it yet getTodasAsSessions.

Show 1 more comment

1 answer

1


You cannot access a server session variable on the client side.

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

  1. Create an Hidden field in HTML and assign the session value to Hidden field on the server side.

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

  2. Create Javascript function to fetch Hidden field value: function getSessionValue() { return documento.getElementById("hfSessionValue").value; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.