public User New() { var user = new User(); user.SessionId = GenerateSessionId(); user.Culture = CultureInfo.CurrentCulture; user.TimeZone = TimeZoneInfo.Local; user.CreateDate = DateTime.UtcNow; user.Items = new Map(); return user; }
User ToSession(Record r) { var user = new User(); user.Id = r.GetInt("id"); user.SessionId = r.GetString("sessionId"); user.Name = r.GetString("name"); user.IdAdmin = r.GetInt("idAdmin"); user.IdSuperAdmin = r.GetInt("idSuperAdmin"); user.IdCustomer = r.GetInt("idCustomer"); var tenant = LoadTenant(r.GetString("tenant")); user.Tenant = tenant; user.Culture = GetCulture(r.GetString("culture"), tenant); user.TimeZone = GetTimeZone(tenant); user.CreateDate = r.GetDateTime("createdate"); var items = r.GetString("items"); user.Items = !string.IsNullOrEmpty(items) ? Json.Deserialize<Map>(items) : new Map(); return user; }
public void DeleteSession(User user) { if (string.IsNullOrEmpty(user.SessionId)) { throw new ArgumentNullException(); } Cache.Default.Remove(GetCacheKey(user.SessionId)); if(this.persistent) { var q = Query.DeleteFrom("session").WhereIsEqual("sessionId", user.SessionId); using(var orm = CreateOrm()) { orm.ExecuteNonQuery(q); } } }
void SaveInDatabase(User user) { var isNew = user.Id == 0; var query = new Query("session"); query.AddValue("name", user.Name); query.AddValue("idAdmin", user.IdAdmin); query.AddValue("idSuperAdmin", user.IdSuperAdmin); query.AddValue("idCustomer", user.IdCustomer); if(user.Tenant != null) { query.AddValue("tenant", user.Tenant.Name); } query.AddValue("culture", user.Culture.Name); query.AddValue("createdate", user.CreateDate); query.AddValue("items", Json.Serialize(user.Items)); if(isNew) { query.AddValue("sessionId", user.SessionId); query.DbOperation = DbOperation.Insert; } else { query.DbOperation = DbOperation.Update; query.WhereIsEqual("sessionId", user.SessionId); } if(this.persistent) { using(var orm = CreateOrm()) { orm.ExecuteNonQuery(query); if(isNew) { user.Id = Convert.ToInt32(orm.DbSession.LastInsertedId); } } } }
void SaveInCookie(MvcContext context, User user, bool isPersistent) { var cookie = new HttpCookie(); cookie.Name = CookieName; cookie.Value = UrlUtil.UrlEncode(user.SessionId); // solo mandar caracteres válidos a la cookie cookie.HttpOnly = true; // si es persistente especifica la fecha, si no dura lo que la sesión del navegador. if (isPersistent) { cookie.Expiration = DateTime.UtcNow.AddDays(CookieExpirationDays); } context.ResponseCookies.Add(CookieName, cookie); }