C# Windows Form - Doubts about Session

Asked

Viewed 456 times

0

I need to save information for as long as the user is logged into my software, I searched and saw that for web pages made with ASP.NET there is the object SESSION, but and for Windows Form?

the information I need and user name, I have its ID inserted in the login screen. but I need to present the user name in a form.

1 answer

3

I found the solution, below:

Source: http://www.linhadecodigo.com.br/artigo/3215/trabalhando-com-sessao-dentro-do-windows-forms.aspx

On the Windows Forms platform people find it difficult to maintain this type of session with variables and information, however, there is a simple way to record session using the desktop platform without any problem.

Working with Session is simple and quiet. Right after logging in with user and password, variables need to be populated and saved in session for future use or verification.

Using Windows Forms, the first step is to create a static public-type class.

Creating Class

Right-click on top of your project or inside an UTIL folder, choose Add / New Item option...

inserir a descrição da imagem aqui

After giving the name (Sessaosistema.Cs) and clicking the Add button, put the following data:

Step 1: Place the word Public Static at the beginning of the class.

public static class SessaoSistema

{}

Step 2: Generate Get and Set variables within the class. They should stay static within the Sessaosistema class.

//usuario
private static Int32 _usuarioId;
private static String _nomeUsuario;
private static String _emailUsuario;

//get e set
public static String UsuarioId
{
   get { return SessaoSistema._usuarioId; }
   set { SessaoSistema._usuarioId = value; }
}
public static String NomeUsuario
{
   get { return SessaoSistema._nomeUsuario; }
   set { SessaoSistema._nomeUsuario = value; }
}
public static String EmailUsuario
{
   get { return SessaoSistema._emailUsuario; }
   set { SessaoSistema._emailUsuario = value; }
}

Using Session Class

Now on the login screen, after you sign in with desktop system user and password just fill in the information as follows.

//depois do login ok
SessaoSistema.NomeUsuario = txtNomeUsuario.text;
SessaoSistema.UsuarioId = usuarioId;
SessaoSistema.EmailUsuario = txtEmail.text;

Anywhere in the system you can pick up the data stored on after the user’s login just by typing and Sessaosistema variable.Property (e.g.: Sessaosistema.Username.

Source: http://www.linhadecodigo.com.br/artigo/3215/trabalhando-com-sessao-dentro-do-windows-forms.aspx

More information:

https://stackoverflow.com/questions/14599127/session-for-windows-forms-application-in-c-sharp

  • I could put the answer here and keep the links just as reference?

  • 1

    Sure. I’ll change it.

Browser other questions tagged

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