Пример #1
0
        public void CerrarSession()
        {
            int idUsuario = int.Parse(HttpContext.Current.Session["idUsuario"].ToString());
            Intranet_FNCEntities ModelBD_Usuario = new Intranet_FNCEntities();
            Log_Acceso_Usuario   log             = (from rt in ModelBD_Usuario.Log_Acceso_Usuario
                                                    where rt.IdUsuario == idUsuario
                                                    orderby rt.IdLog descending
                                                    select rt).First();

            TimeSpan hi   = TimeSpan.Parse((log.FechaInicioSession).ToString("HH:mm:ss"));
            TimeSpan hf   = TimeSpan.Parse(DateTime.Now.ToString("HH:mm:ss"));
            TimeSpan time = hf - hi;

            log.FechaFinalizoSession = DateTime.Now;
            log.TiempoSession        = time;
            ModelBD_Usuario.SaveChanges();
            if (HttpContext.Current.Request.Cookies["idUsuario"] != null)
            {
                HttpContext.Current.Response.Cookies["idUsuario"].Expires = DateTime.Now.AddDays(-1);
            }
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Session.Clear();
        }
Пример #2
0
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            LdapAuthentication adAuth = new LdapAuthentication();
            int    i    = 0;
            string User = Request.Form["Username"];
            string Pass = Request.Form["Password"];

            try
            {
                if (true == adAuth.IsAuthenticated(User, Pass))
                {
                    String            groups           = adAuth.GetGroups();
                    DirectoryEntry    myLdapConnection = adAuth.createDirectoryEntry(User, Pass);
                    DirectorySearcher search           = new DirectorySearcher(myLdapConnection);
                    search.Filter = "(samaccountname=" + User + ")";
                    SearchResult result = search.FindOne();
                    if (result != null)
                    {
                        DirectoryEntry usuario = result.GetDirectoryEntry();
                        string         Guid    = usuario.Guid.ToString();

                        Intranet_FNCEntities ModelBD_Usuario = new Intranet_FNCEntities();
                        UsuarioRol           Rol             = (from u in ModelBD_Usuario.Usuario
                                                                join r in ModelBD_Usuario.UsuarioRol on u.IdUsuario equals r.IdUsuario
                                                                join q in ModelBD_Usuario.TipoRol on r.IdTipoRol equals q.IdTipoRol
                                                                where u.IdRegistro_AD == Guid
                                                                select r).First();

                        List <int> ModulosRol = (from r in ModelBD_Usuario.Acceso
                                                 where r.IdUsuario == Rol.IdUsuario
                                                 select r.IdModulo.Value).ToList();

                        int Rolac = Rol.IdTipoRol;
                        Session["idUsuario"]      = Rol.IdUsuario;
                        Session["Rol"]            = Rolac;
                        Session["Modulos"]        = ModulosRol;
                        Session["NombreCompleto"] = usuario.Properties["displayname"].Value.ToString();
                        Session["Nombre"]         = usuario.Properties["givenName"].Value.ToString();
                        Session["Titulo"]         = usuario.Properties["title"].Value.ToString();
                        Session["Unidad"]         = usuario.Properties["department"].Value.ToString();
                        Session["Compañia"]       = usuario.Properties["company"].Value.ToString();

                        Log_Acceso_Usuario log_ = new Log_Acceso_Usuario();
                        log_.IdUsuario          = Rol.IdUsuario;
                        log_.FechaInicioSession = DateTime.Now;
                        ModelBD_Usuario.Log_Acceso_Usuario.Add(log_);
                        ModelBD_Usuario.SaveChanges();

                        //Create the ticket, and add the groups.
                        //bool isCookiePersistent = chkPersist.Checked;
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, User,
                                                                                             DateTime.Now, DateTime.Now.AddMinutes(0), false, groups);

                        //Encrypt the ticket.
                        String encryptedTicket = FormsAuthentication.Encrypt(authTicket);

                        //Create a cookie, and then add the encrypted ticket to the cookie as data.
                        HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                        //if (true == isCookiePersistent)
                        //    authCookie.Expires = authTicket.Expiration;

                        //Add the cookie to the outgoing cookies collection.
                        Response.Cookies.Add(authCookie);

                        //You can redirect now.
                        Response.Redirect(FormsAuthentication.GetRedirectUrl(User, false));
                    }
                }
                else
                {
                    i++;
                }
            }
            catch (Exception ex)
            {
                if (i != 0)
                {
                    errorLabel.Visible = true;
                    errorLabel.Text    = "Error: " + ex;
                }
                else
                {
                    if (User == "")
                    {
                        errorLabel.Visible = true;
                        errorLabel.Text    = "Se requiere un Usuario";
                    }
                    else if (Pass == "")
                    {
                        errorLabel.Visible = true;
                        errorLabel.Text    = "Se requiere una Contraseña.";
                    }
                    else
                    {
                        errorLabel.Visible = true;
                        errorLabel.Text    = "Error de autenticación, por favor verifique su nombre de Usuario y Contraseña. Si el error persiste por favor contactarse con Dirección de Tecnología.";
                    }
                }
            }
        }