Added more cookie extensions and using set cookie now.

This commit is contained in:
MattMo 2022-02-06 22:50:22 -08:00
parent b7837f6639
commit bf1b66e565
2 changed files with 78 additions and 5 deletions

View File

@ -93,10 +93,83 @@ namespace MontoyaTech.Rest.Net
/// <returns>This response.</returns> /// <returns>This response.</returns>
public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value) public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value)
{ {
if (response.Cookies[name] == null) response.SetCookie(new Cookie(name, value));
response.Cookies.Add(new Cookie(name, value));
else return response;
response.Cookies[name].Value = value; }
/// <summary>
/// Sets a cookie for a given response.
/// </summary>
/// <param name="response"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
/// <returns>This response.</returns>
public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value, DateTime expires)
{
response.SetCookie(new Cookie(name, value) { Expires = expires });
return response;
}
/// <summary>
/// Sets a cookie for a given response.
/// </summary>
/// <param name="response"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="httpOnly"></param>
/// <returns>This response.</returns>
public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value, bool httpOnly)
{
response.SetCookie(new Cookie(name, value) { HttpOnly = httpOnly });
return response;
}
/// <summary>
/// Sets a cookie for a given response.
/// </summary>
/// <param name="response"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="httpOnly"></param>
/// <param name="secure"></param>
/// <returns>This response.</returns>
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;
}
/// <summary>
/// Sets a cookie for a given response.
/// </summary>
/// <param name="response"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="httpOnly"></param>
/// <param name="secure"></param>
/// <param name="expires"></param>
/// <returns>This response.</returns>
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;
}
/// <summary>
/// Sets a cookie for a given response.
/// </summary>
/// <param name="response"></param>
/// <param name="cookie"></param>
/// <returns>This response.</returns>
public static HttpListenerResponse WithCookie(this HttpListenerResponse response, Cookie cookie)
{
response.SetCookie(cookie);
return response; return response;
} }

View File

@ -14,7 +14,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName> <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace> <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile> <GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.0.2</Version> <Version>1.0.3</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json"> <PackageReference Include="Newtonsoft.Json">