public override string ToString() { return(Idp.ToString() + " " + DatumVremeKreiranja.ToShortDateString() + " " + IdKupca.ToString()); }
public async Task <Session> CreateSessionAsync(string user, Idp idp, bool longSession = false, string ip = null, string userAgent = null, bool isTech = false) { string sessionId; Session session = null; using (DB db = await DB.CreateAsync(dbUrl)) { var sb = new StringBuilder(); foreach (var b in BitConverter.GetBytes(DateTime.Now.Ticks)) { sb.Append(b.ToString("X2")); } int tryCount = 0; do { sessionId = sb + StringExt.RandomSecureString(10); try { session = new Session { id = sessionId, user = user, idp = idp, ip = ip, user_agent = userAgent, long_session = longSession, tech = isTech, duration = longSession ? TimeSpan.FromSeconds(sessionLongTimeout) : TimeSpan.FromSeconds(sessionTimeout) }; if (!await session.InsertAsync(db)) { sessionId = null; } } catch (MySql.Data.MySqlClient.MySqlException e) { // if it is not a duplicate ticketId re throw the Exception if (e.Number != 1062) { throw; } sessionId = null; } tryCount++; }while (sessionId == null && tryCount < 10); if (sessionId == null) { throw new Exception("Session create fails. Impossible generate a sessionId"); } // if its not a long session, only 1 short session is allowed at a time // invalidate previous sessions if (!isTech && !longSession) { try { await db.DeleteAsync($"DELETE FROM `session` WHERE `{nameof(session.long_session)}` = FALSE AND `{nameof(session.tech)}` = FALSE AND `{nameof(session.id)}` != ? AND {nameof(session.user)} = ?", sessionId, user); } catch (MySql.Data.MySqlClient.MySqlException e) { // if a dead lock is detected while deleting, accept // to delete later. Else re throw the exception if (e.Number != 1213) { throw; } } } } return(session); }