C# Windows Forms - Getting the ID of a Logged-in User

Asked

Viewed 1,032 times

-1

I need to take the ID of an already logged in user to create another product. How do I do this ?

Login do Usuário

Cadastro do produto

So, soon, and with the email and password logged in, I want to find the ID logged in for when I register the car, it pull this ID and register. I’ve tried using Thread.CurrentPrincipal.Identity.Isauthenticated, Environment.Username and the Windowsidentity.Getcurrent() but these only take the name of the person logged in windows, not in the application.

Login

        Empresa empresa = new Empresa();
        empresa.Email = txtEmail.Text;
        empresa.Senha = txtSenha.Text;
        if(EmpresaDAO.BuscarEmpresaPorEmailESenha(empresa) != null)
        {
            empresa = EmpresaDAO.BuscarEmpresaPorEmailESenha(empresa);
            this.Close();
            MenuEmpresa menuEmpresa = new MenuEmpresa();
            menuEmpresa.ShowDialog();  
        }

Register of the Car

          try
            {
                if (WindowsIdentity.GetCurrent().IsAuthenticated)
                {
                   empresa.Email = WindowsIdentity.GetCurrent().Name;
                }
                carro.Empresa = EmpresaDAO.BuscarEmpresaPorEmail(empresa);
                carro.Nome = txtNome.Text;
                carro.Cambio = txtCambio.Text;
                carro.Cor = txtCor.Text;
                carro.Marca = txtMarca.Text;
                carro.Quilometragem = txtQuilometragem.Text;
                carro.Placa = txtPlaca.Text;
                carro.Portas = int.Parse(txtPorta.Text);
                carro.Ano = int.Parse(txtPorta.Text);
                carro.Preco = txtPreco.Text;
                CarroDAO.Incluir(carro);

                MessageBox.Show("O cadastro do carro: " + carro.Nome + " foi concluido com sucesso", "Cadastrado");
            }

I tried to do as much as possible to Asp.NET, because I didn’t understand how I’m going to do this in C#.

ASP.NET

Usuario usu = new Usuario();
            ItemVenda itemv = new ItemVenda();
            if (Request.IsAuthenticated)
            {
                usu.Login = HttpContext.User.Identity.Name;
            }
            venda.Usuario = UsuarioDAO.BuscarUsuarioPorLogin(usu);
  • Present the context of your problem, this should include the source code that is having problems and exactly what you tried to do

  • I put the code I’m trying to make.

  • @Osvaldojunior is programming for web or desktop?

  • @Desktop cat, but I already managed to fix.

1 answer

1


Believing that the page that has to register the car is the MenuEmpresa pass user login which is the variable empresa per parameter at the time of constructing the object MenuEmpresa:

In the login snippet:

Empresa empresa = new Empresa();
empresa.Email = txtEmail.Text;
empresa.Senha = txtSenha.Text;
if(EmpresaDAO.BuscarEmpresaPorEmailESenha(empresa) != null)
{
    empresa = EmpresaDAO.BuscarEmpresaPorEmailESenha(empresa);
    this.Close();
    MenuEmpresa menuEmpresa = new MenuEmpresa(empresa); // <<<<<<<<< AQUI
    menuEmpresa.ShowDialog();  
}

But the ideal would be to work with some kind of session, I’ll leave it for the C# devs to complete it!

Ah! and remember in the class builder MinhaEmpresa there must be a declaration of the variable empresa

  • Vlw manooo, I was able to do it the way you said by passing the variable by parameter at the time of constructing the object.

Browser other questions tagged

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