Advertisement
panxop

Untitled

Apr 26th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using dim.rutinas;
  6.  
  7. namespace WebProveedoresNet.Cs
  8. {
  9.     [Serializable]
  10.     public class ValidaSesion
  11.     {
  12.         ClsLog log = new ClsLog();
  13.         public void registrarSesion(HttpContext contexto, string nombreusuario)
  14.         {
  15.             TimeSpan TimeOut = new TimeSpan(0, 0, contexto.Session.Timeout, 0, 0);            
  16.  
  17.             nombreusuario = nombreusuario.Trim();
  18.  
  19.             if (contexto.Cache[nombreusuario] == null) //valida si la sesión esta libre.
  20.                 contexto.Cache.Insert(nombreusuario,
  21.                 contexto.Session.SessionID,
  22.                 null,
  23.                 DateTime.Now.AddMinutes(contexto.Session.Timeout),
  24.                 System.Web.Caching.Cache.NoSlidingExpiration,
  25.                 System.Web.Caching.CacheItemPriority.NotRemovable,
  26.                 null);
  27.             contexto.Session["nombreusuario"] = nombreusuario;  //Guarda el nombre de usuario actual.
  28.  
  29.         }//Fin de funcion.
  30.  
  31.  
  32.         public bool HayConcurrencia(HttpContext contexto)
  33.         {
  34.             bool result = false; //Por default se indica que solo hay una sesion concurrente.
  35.  
  36.             if (contexto.Session != null)
  37.             {
  38.  
  39.                 if (contexto.Session["nombreusuario"] != null)
  40.                 {
  41.  
  42.                     string cacheKey = contexto.Session["nombreusuario"].ToString();
  43.                     result = ((string)contexto.Cache[cacheKey] != contexto.Session.SessionID);
  44.                     if (!result)
  45.                     {
  46.                         string user = (string)contexto.Cache[cacheKey]; //Esta linea resetea a cero el periodo de cache.
  47.                     }
  48.                 }
  49.             }
  50.            
  51.             return result;
  52.         }
  53.  
  54.  
  55.         //Como ven el webmehtod hace referencia a otro metodo llamado EliminarRegistro, el cual es el siguiente:      
  56.         public void EliminarRegistro(HttpContext contexto)
  57.         {
  58.             if (contexto.Session["nombreusuario"] != null)
  59.                 contexto.Cache.Remove(contexto.Session["nombreusuario"].ToString());
  60.         }//fin de funcion.
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement