Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Last active October 11, 2020 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/79d386c3c0a53c93b8ef83fb57622fa5 to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/79d386c3c0a53c93b8ef83fb57622fa5 to your computer and use it in GitHub Desktop.
A simple middleware which adds a dummy cookie to the response
public class DummyCookieMiddleware
{
private readonly RequestDelegate _next;
public DummyCookieMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
// Check if cookie is already present
var dummyCookieValue = context.Request.Cookies["DummyCookie"];
var isValidDummyCookieValue = Guid.TryParse(dummyCookieValue, out Guid result);
// If not present then add new value
if(!isValidDummyCookieValue) {
context.Response.Cookies.Append("DummyCookie", Guid.NewGuid().ToString());
}
// handover request to next delegate in pipeline
return _next(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment