Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/09/2015, 10:28
jmfmagnum
 
Fecha de Ingreso: julio-2014
Mensajes: 116
Antigüedad: 9 años, 10 meses
Puntos: 1
Login con asp.net y SQL usando C#

Buenas tardes, lamento la intormision, no estoy seguro si este es el lugar adecuado del foro para consultar lo que pregunto en el titulo
ok tengo hacer un login en visual studio usando las reglas de mi profesor

Archivo login



Código ASP:
Ver original
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6.  
  7. <head runat="server">
  8. <style type="text/css">
  9. .normal {
  10.   width: 250px;
  11.   border: 1px solid #000;
  12. }
  13. .normal th, .normal td {
  14.   border: 1px solid #000;
  15. }
  16. </style>
  17.     <title></title>
  18. </head>
  19. <body>
  20.     <form id="form1" runat="server">
  21.     <div>
  22.      <table class="normal" >
  23.       <th scope="col">Usuario</th>
  24.     <th>
  25.         <asp:TextBox ID="txtUsuario" runat="server"></asp:TextBox>
  26.       </th>
  27.       <tr></tr>
  28.         <th scope="col">Clave</th>
  29.     <th>
  30.         <asp:TextBox ID="txtClave" runat="server"></asp:TextBox>
  31.       </th>
  32.  
  33.     </table>
  34.        <asp:Button ID="Button1" runat="server" Text="Entrar"
  35.         PostBackUrl="~/procesarLogin.aspx" />
  36.     </div>
  37.  
  38.     </form>
  39. </body>
  40. </html>


Y aqui la madre del cordero:
La regla de mi profesor es enviar usando un "PostBackUrl" a un archivo para que otro aspx haga el login
Este el "Procesarlogin.aspx" es el que no se hacer , el codigo no se como arreglarlo.
Código vb:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8.  
  9. public partial class procesarLogin : System.Web.UI.Page
  10. {
  11.     protected void Page_Load(object sender, EventArgs e)
  12.     {
  13.         String user = Request["txtUsuario"];
  14.         String clave = Request["txtClave"];
  15.         Conexion con = new Conexion();
  16.         con.conectar();
  17.         /// que debe hacer, tomar los label y si esta en la BD, dejar pasar
  18.          
  19.         try
  20.         {
  21.             if (con.validar(user) > 0)
  22.             {
  23.                 Response.Redirect("menu.aspx");
  24.             }
  25.             else
  26.             {
  27.                 Response.Write("No se encontro usuario");
  28.             }
  29.  
  30.         }
  31.         catch(SqlException ex)
  32.         {
  33.  
  34.             Response.Write("No se puede ejecutar" + ex.Message);
  35.         }
  36.  
  37.  
  38.         con.desconectar();
  39.  
  40.    
  41.     }
  42. }

La clase que hace todo por si acaso


Código ASP:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data.SqlClient;
  6.  
  7. /// <summary>
  8. /// Summary description for Conexion
  9. /// </summary>
  10. public class Conexion
  11. {
  12.      SqlConnection conn;
  13.      SqlCommand comando;
  14.     public Conexion()
  15.       {
  16.           conn = new SqlConnection("Data Source=VESTERGAARD-PC\\VESTERGAARD;Initial Catalog=crud;User ID=sa;Password=123456");
  17.        }
  18.  
  19.     public String conectar()
  20.     {
  21.  
  22.         try
  23.         {
  24.             conn.Open();
  25.         }
  26.         catch (SqlException e)
  27.         {
  28.             return "Error:" + e.Message;
  29.         }
  30.         return "Conexion exitosa";
  31.    
  32.     }
  33.  
  34.  
  35.     public String desconectar()
  36.     {
  37.  
  38.         try
  39.         {
  40.             conn.Close();
  41.         }
  42.         catch (SqlException e)
  43.         {
  44.             return "Error:" + e.Message;
  45.         }
  46.         return "Desconexion exitosa";
  47.  
  48.     }
  49.  
  50.  
  51.    
  52.  
  53.   public int validar(String user, String clave)
  54.   {
  55.       comando = new SqlCommand("Select * from usuario where usuario='"+user+"'and clave='"+clave+"'");
  56.        return comando.ExecuteNonQuery();
  57.      }
  58. }
por su atencion gracias, ojala alguien pueda ayudarme

Última edición por jmfmagnum; 01/10/2015 a las 15:49