RobertoCarlos Melgar

RobertoCarlos Melgar

  • 1.5k
  • 159
  • 9.3k

get UserId and nameId with Entity Framework login

Apr 25 2020 2:00 PM
Good afternoon, I am working with Entity Framework, C # and Sql server, in a project, I have this method for login, which works correctly. 
  1. public static bool ValidarUsuario(string Usuario, string Password)  
  2. {  
  3.     using (GourmetEntities db = new GourmetEntities())  
  4.     {  
  5.         // bcryp para encriptar, buscar información  
  6.         var Usu = from u in db.tblUsuarios  
  7.                   where u.Usuario == Usuario && u.Password == Password  
  8.                   select new { u.Usuario,u.Perfil_Id};  
  9.         if (Usu.Count() > 0)  
  10.         {  
  11.             return true;  
  12.         }  
  13.         else  
  14.         {  
  15.             return false;  
  16.         }           
  17.     }  
  18. }  
but after that I need to get the UserId, UserName, and profileId to perform other operations, and I would like to save them in session variables, please how can I do that, since the return of my method is bool. This is the code of my button
  1. private void BtnIngresar_Click(object sender, EventArgs e)  
  2. {  
  3.     if(CapaDatos.tblUsuario.ValidarUsuario(TxtUsuario.Text, TxtPassword.Text))  
  4.     {  
  5.         FrmPadre frmPadre = new FrmPadre();  
  6.         frmPadre.Show();  
  7.     }  
  8.     else  
  9.     {  
  10.         return;  
  11.     }  
  12. }  
Now with Entity Framework and linq I generate this sql server code which brings me the data I need,
but I don't know how to get it in c #
  1. declare @p__linq__0 varchar(30) set @p__linq__0 = 'rmelgar';  
  2. declare @p__linq__1 varchar(30)  set @p__linq__1 = '123456789sc';  
  3. SELECT   
  4.     1 AS [C1],   
  5.     [Extent1].[Usuario] AS [Usuario],   
  6.     [Extent1].[Perfil_Id] AS [Perfil_Id]  
  7.     FROM [dbo].[tblUsuarios] AS [Extent1]  
  8.     WHERE ([Extent1].[Usuario] = @p__linq__0) AND ([Extent1].[Password] = @p__linq__1)  
Please can you help me solve my problem, thank you very much
 
Please excuse me, I still don't know how to use the forum, I think I have formats that should not be  
 
Roberto  

Answers (8)