Example #1
0
 /// <summary>
 /// Performs the login into the application.
 /// </summary>
 /// <param name="login">Login of the user</param>
 /// <param name="password">Password of the user</param>
 /// <returns>An User DTO representing the logged user and session if the login 
 ///     is ok, null otherwise</returns>
 public static RecursoDTO Login(string login, string password)
 {
     using (EntitiesModel context = new EntitiesModel())
     {
         // Retrieve user from database with the given login/pass combination
         WEB_RECURSO recurso = context.WEB_RECURSOs.Where(p => p.EMAIL == login && p.PASS_RECURSO == password).FirstOrDefault();
         // If the combination user/pass is not correct, then return null information.
         if (recurso == null)
         {
             return null;
         }
         // Create a new session token GUID without scores.
         string guid = Guid.NewGuid().ToString().Replace("-", "");
         // Build the user information DTO from the user entity and the session token
         RecursoDTO result = new RecursoDTO();
         result.Fill(recurso);
         result.SessionToken = guid;
         // Remove other sessions of this user
         WEB_SESSION session = context.WEB_SESSIONs.Where(p => p.User_id == recurso.ID_RECURSO).FirstOrDefault();
         if (session != null)
         {
             context.Delete(session);
         }
         // Build the session entity
         session = new WEB_SESSION();
         session.Session_guid = result.SessionToken;
         session.User_id = result.Id_Recurso;
         session.Creation_dttm = DateTime.UtcNow;
         // Insert the session into database
         context.Add(session);
         // Commit changes
         context.SaveChanges();
         return result;
     }
 }
Example #2
0
 /// <summary>
 /// Performs the logout from a session token.
 /// </summary>
 /// <param name="sessionToken">Session token GUID</param>
 public static void Logout(string sessionToken)
 {
     using (EntitiesModel context = new EntitiesModel())
     {
         WEB_SESSION session = context.WEB_SESSIONs.Where(p => p.Session_guid == sessionToken).FirstOrDefault();
         if (session != null)
         {
             context.Delete(session);
             context.SaveChanges();
         }
     }
 }