MVC in C # how the statements work

1

I have the code below, and to call the session that stores the user data, only once do I want to put it in the controller declaration.

Doubt

The variable usuarioLogado is per request or it can happen that user X calls the controller and in the middle user Y calls the same controller, and usuarioLogado information exchange, where should read user X, step the user Y.

Code

public class AppRequisitoController : Controller
{
    string controller = "AppRequisito";
    UsuarioLogadoDTO usuarioLogado = Services.UsuarioService.SessaoUsuarioLogado();

    private void Listagem(Conexao db)
    {   
        var repAppRequisito = new AppRequisitoRepositorio(db);
        ViewBag.ListaAppRequisito = repAppRequisito.ComboBox(usuarioLogado);
    }

User session

public static UsuarioLogadoDTO SessaoUsuarioLogado()
{
    return HttpContext.Current.Session[Constantes.sessaoUsuarioLogado] as UsuarioLogadoDTO;
}
    
asked by anonymous 30.03.2017 / 16:08

2 answers

2

Interactions in web applications can be Stateless or Statefull , that is, it does not maintain application states or keeps track of the state of the interactions. In Stateless requests the states are kept in the client, through cookies or otherwise, always in encrypted form.

A Session on the other hand keeps the state on the server different from the cookie, so Statefull , it works as follows, your application creates a key that references the data that is temporarily held on the server . Then it sends a cookie or other form of authentication to the client, which references the data stored on the server.

So it would not occur if the user receives data from a session of another user because it is referenced by that unique key.

    
30.03.2017 / 16:23
1

It is always by request. This does not only apply to ASP.NET MVC, this is how a web application works.

Basically, no server-side state is maintained. So if you use strategies like cookies to simulate this kind of thing, cookies are sent on all requests and handled on the server.

    
30.03.2017 / 16:13