From bf1b66e565eb34cda016209ef568fb03b5e96e35 Mon Sep 17 00:00:00 2001 From: MattMo Date: Sun, 6 Feb 2022 22:50:22 -0800 Subject: [PATCH] Added more cookie extensions and using set cookie now. --- .../HttpListenerResponseExtensions.cs | 81 ++++++++++++++++++- Rest.Net/Rest.Net/Rest.Net.csproj | 2 +- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs b/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs index d18c596..694ff4b 100644 --- a/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs +++ b/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs @@ -93,10 +93,83 @@ namespace MontoyaTech.Rest.Net /// This response. public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value) { - if (response.Cookies[name] == null) - response.Cookies.Add(new Cookie(name, value)); - else - response.Cookies[name].Value = value; + response.SetCookie(new Cookie(name, value)); + + return response; + } + + /// + /// Sets a cookie for a given response. + /// + /// + /// + /// + /// + /// This response. + public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value, DateTime expires) + { + response.SetCookie(new Cookie(name, value) { Expires = expires }); + + return response; + } + + /// + /// Sets a cookie for a given response. + /// + /// + /// + /// + /// + /// This response. + public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value, bool httpOnly) + { + response.SetCookie(new Cookie(name, value) { HttpOnly = httpOnly }); + + return response; + } + + /// + /// Sets a cookie for a given response. + /// + /// + /// + /// + /// + /// + /// This response. + public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value, bool httpOnly, bool secure) + { + response.SetCookie(new Cookie(name, value) { HttpOnly = httpOnly, Secure = secure }); + + return response; + } + + /// + /// Sets a cookie for a given response. + /// + /// + /// + /// + /// + /// + /// + /// This response. + public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value, bool httpOnly, bool secure, DateTime expires) + { + response.SetCookie(new Cookie(name, value) { HttpOnly = httpOnly, Secure = secure, Expires = expires }); + + return response; + } + + /// + /// Sets a cookie for a given response. + /// + /// + /// + /// This response. + public static HttpListenerResponse WithCookie(this HttpListenerResponse response, Cookie cookie) + { + response.SetCookie(cookie); return response; } diff --git a/Rest.Net/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net/Rest.Net.csproj index 84ab016..4ca6c3d 100644 --- a/Rest.Net/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net/Rest.Net.csproj @@ -14,7 +14,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.0.2 + 1.0.3