private TMessage Get(bool delete) { if (RouteBinding.TenantName.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(RouteBinding.TenantName), RouteBinding.GetTypeName()); } if (RouteBinding.TrackName.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(RouteBinding.TrackName), RouteBinding.GetTypeName()); } if (RouteBinding.UpParty == null) { throw new ArgumentNullException(nameof(RouteBinding.UpParty), RouteBinding.GetTypeName()); } logger.ScopeTrace($"Get Cookie '{typeof(TMessage).Name}', Route '{RouteBinding.Route}', Delete '{delete}'."); var cookie = httpContextAccessor.HttpContext.Request.Cookies[CookieName()]; if (!cookie.IsNullOrWhiteSpace()) { try { var envelope = CookieEnvelope <TMessage> .FromCookieString(CreateProtector(), cookie); if (delete) { logger.ScopeTrace($"Delete Cookie, '{typeof(TMessage).Name}', Route '{RouteBinding.Route}'."); DeleteByName(CookieName()); } return(envelope.Message); } catch (CryptographicException ex) { logger.Warning(ex, $"Unable to Unprotect Cookie '{typeof(TMessage).Name}', deleting cookie."); DeleteByName(CookieName()); return(null); } catch (Exception ex) { throw new Exception($"Unable to Read Cookie '{typeof(TMessage).Name}'.", ex); } } else { return(null); } }
private TMessage Get(bool delete, bool tryGet = false) { if (tryGet && RouteBindingDoNotExists()) { return(null); } CheckRouteBinding(); logger.ScopeTrace($"Get Cookie '{typeof(TMessage).Name}', Route '{RouteBinding.Route}', Delete '{delete}'."); var cookie = httpContextAccessor.HttpContext.Request.Cookies[CookieName()]; if (!cookie.IsNullOrWhiteSpace()) { try { var envelope = CookieEnvelope <TMessage> .FromCookieString(CreateProtector(), cookie); if (delete) { logger.ScopeTrace($"Delete Cookie, '{typeof(TMessage).Name}', Route '{RouteBinding.Route}'."); DeleteByName(CookieName()); } return(envelope.Message); } catch (CryptographicException ex) { logger.Warning(ex, $"Unable to Unprotect Cookie '{typeof(TMessage).Name}', deleting cookie."); DeleteByName(CookieName()); return(null); } catch (Exception ex) { throw new Exception($"Unable to Read Cookie '{typeof(TMessage).Name}'.", ex); } } else { return(null); } }