Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/12/2010, 12:55
estlui
 
Fecha de Ingreso: noviembre-2003
Mensajes: 209
Antigüedad: 20 años, 5 meses
Puntos: 0
Una ayudita con este ejemplo, por favor...

Buenos dias...

Intente aplicar este ejemplo en un proyecto que tengo: http://www.webprogramacion.com/77/cs...sitio-web.aspx

La manera que lo aplique fue:

- Base de datos en SQL con una tabla llamada Usuarios y con estos campos:

Usuario
Clave
Nombre
Rol

Actualmente solo tengo 2 usuarios, uno con el rol llamado: Req y el otro con: Apr

- En el web.config lo tengo asi:

Código:
<?xml version="1.0"?>

<configuration>

    <appSettings/>
    <connectionStrings/>
    <system.web>
      <customErrors mode="Off" />    
              
        <compilation debug="true">
        </compilation>
       
      <authentication mode="Forms">
        <forms name =".ASPXFORMSDEMO" loginUrl="login.aspx" protection="All" path="/" timeout="30" />
        <passport redirectUrl="internal"/>
      </authentication>

      <authorization>
        <allow users="*"/>
      </authorization>                
     </system.web>

  <location path="odc/Aprobador">
    <system.web>
      <authorization>
        <deny users="?"/>
        <deny roles="Req"/>
        <allow roles="Apr"/>
      </authorization>
    </system.web>
  </location>

  <location path="odc/Requisitor">
    <system.web>
      <authorization>
        <deny users="?"/>
        <deny roles="Apr"/>
        <allow roles="Req"/>
      </authorization>
    </system.web>
  </location>

</configuration>
- En mi pagina de login, use el componente, tengo esto:

Código:
Protected Sub acceso_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles acceso.Authenticate
        Dim conexion As New ClassConexion
        Dim cmd As New SqlCommand()
        cmd.CommandText = "SP_Usuarios"
        cmd.CommandType = Data.CommandType.StoredProcedure
        cmd.Connection = conexion.siteConexion
        cmd.Parameters.AddWithValue("@usuario", acceso.UserName)
        cmd.Parameters.AddWithValue("@clave", acceso.Password)
        conexion.siteConexion.Open()
        If cmd.ExecuteScalar() = 1 Then
            cmd.Parameters.Clear()
            cmd.CommandText = "SP_Rol"
            cmd.Parameters.AddWithValue("@usuario", acceso.UserName)
            Dim Rol As String
            Dim tabla As New DataTable
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(tabla)
            Rol = tabla.Rows(0)("Rol").ToString

            'Creando Cookie
            Dim tkt As FormsAuthenticationTicket
            Dim cook As String, sNombre As String
            Dim ck As HttpCookie
            sNombre = acceso.UserName
            tkt = New FormsAuthenticationTicket(1, sNombre, DateTime.Now, DateTime.Now.AddMinutes(30), False, Rol & "")
            cook = FormsAuthentication.Encrypt(tkt)
            ck = New HttpCookie(FormsAuthentication.FormsCookieName, cook)
            Page.Response.Cookies.Add(ck)
        Else
        End If
        conexion.siteConexion.Close()
    End Sub
y en el archivo Global.asax , tengo esto:

Código:
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        If Not (HttpContext.Current.User Is Nothing) Then
            If HttpContext.Current.User.Identity.AuthenticationType = "Forms" Then
                Dim tkt As FormsAuthenticationTicket
                tkt = FormsAuthentication.Decrypt(Request.Cookies(FormsAuthentication.FormsCookieName).Value)
                Dim usuarios As String() = {""}
                
                If tkt.UserData = "Req" Then
                    usuarios(0) = "Req"
                Else
                    If tkt.UserData = "Apr" Then
                        usuarios(0) = "Apr"
                    Else
                        Response.Redirect("http://www.google.com.mx")
                    End If
                End If
                Dim id As System.Web.Security.FormsIdentity
                id = DirectCast(HttpContext.Current.User.Identity, System.Web.Security.FormsIdentity)
                HttpContext.Current.User = New System.Security.Principal.GenericPrincipal(id, usuarios)
            End If
        End If
    End Sub
- Lo probe y no me funciona!!! y es aqui donde necesito su ayuda....

Lo que noto es que al probar la pagina se ejecuta el global.asax, verifica el primer IF y al no encontrar algo.... que no se que sea ..... se salta todo el codigo.

Despues me pide el usuario y la clave, despues de ingresarlos y darle clic a aceptar, vuelve a correr el archivo global.asax y de nuevo al comparar el primer IF y se salta todo el codigo.

Y despues ejecuta el codigo del archivo Login.aspx y finaliza diciendo: Your login attempt was not successful. Please try again.

Que esta mal?

Muchas gracias