From 2c519bf1220f7b8465f611f6d92323926155eaeb Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sun, 28 May 2023 05:09:18 +0200 Subject: [PATCH] Add cookie support on IWebContext. --- .../Code/AspnetCoreWebContext.cs | 31 +++++++++++++++++++ VAR.WebFormsCore/Code/IWebContext.cs | 8 +++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/VAR.WebFormsCore.AspNetCore/Code/AspnetCoreWebContext.cs b/VAR.WebFormsCore.AspNetCore/Code/AspnetCoreWebContext.cs index 594155a..0c0e912 100644 --- a/VAR.WebFormsCore.AspNetCore/Code/AspnetCoreWebContext.cs +++ b/VAR.WebFormsCore.AspNetCore/Code/AspnetCoreWebContext.cs @@ -36,6 +36,23 @@ public class AspnetCoreWebContext : IWebContext } } + + private Dictionary? _requestCookies; + + public Dictionary RequestCookies + { + get + { + if (_requestCookies == null) + { + _requestCookies = _context.Request.Cookies + .ToDictionary(p => p.Key, p => p.Value); + } + + return _requestCookies; + } + } + private Dictionary? _requestQuery; public Dictionary RequestQuery @@ -95,6 +112,20 @@ public class AspnetCoreWebContext : IWebContext _context.Response.Redirect(url); } + public void AddResponseCookie(string cookieName, string value, DateTime? expiration = null) + { + _context.Response.Cookies.Append( + key: cookieName, + value: value, + options: new CookieOptions {Expires = expiration,} + ); + } + + public void DelResponseCookie(string cookieName) + { + _context.Response.Cookies.Delete(cookieName); + } + public bool ResponseHasStarted => _context.Response.HasStarted; public int ResponseStatusCode diff --git a/VAR.WebFormsCore/Code/IWebContext.cs b/VAR.WebFormsCore/Code/IWebContext.cs index 985e50c..69ae035 100644 --- a/VAR.WebFormsCore/Code/IWebContext.cs +++ b/VAR.WebFormsCore/Code/IWebContext.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace VAR.WebFormsCore.Code; @@ -6,14 +7,17 @@ public interface IWebContext { string RequestPath { get; } string RequestMethod { get; } - Dictionary RequestForm { get; } - Dictionary RequestQuery { get; } Dictionary RequestHeader { get; } + Dictionary RequestCookies { get; } + Dictionary RequestQuery { get; } + Dictionary RequestForm { get; } void ResponseWrite(string text); void ResponseWriteBin(byte[] content); void ResponseFlush(); void ResponseRedirect(string url); + void AddResponseCookie(string cookieName, string value, DateTime? expiration = null); + void DelResponseCookie(string cookieName); bool ResponseHasStarted { get; } int ResponseStatusCode { get; set; }