public async Task <UserSession> GetAsync(string id) { var value = await _distributedCache.GetStringAsync(GetSessionKey(id)); var data = ProtectionUtils.DeserializeAndUnprotect <IDictionary <string, string> >(value, _dataProtector); return(new UserSession(id, data)); }
public void CreateIdCookie(UserSession session) { var cookieOptions = CreateCookieOptions(); var value = ProtectionUtils.SerializeAndProtect(session.Id, _dataProtector); HttpContext.Response.Cookies.Append(CookieName, value, cookieOptions); }
public string GetIdFromCookie() { if (!IsCookieExist) { return(null); } if (!HttpContext.Request.Cookies.TryGetValue(CookieName, out var value)) { return(null); } return(string.IsNullOrWhiteSpace(value) ? null : ProtectionUtils.DeserializeAndUnprotect <string>(value, _dataProtector)); }
public Task SetAsync(UserSession userSession) { if (userSession == null) { throw new ArgumentNullException(nameof(userSession)); } var id = userSession.Id; if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentException("Session id is empty."); } var session = ProtectionUtils.SerializeAndProtect(userSession.Data, _dataProtector); return(_distributedCache.SetStringAsync(GetSessionKey(id), session)); }