Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of creating cookies inside a Azure Function
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
namespace FunctionCookieExample
{
public static class MyFunction
{
[FunctionName("MyFunction")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req)
{
// Read a cookie
var cookieValue = req.Cookies["MyCookie"];
// Set a cooke
//// Configure the cookie
CookieOptions option = new CookieOptions();
option.Expires = DateTime.Now.AddYears(1);
option.Domain = ".mydomain.tld";
//// Make the cookie available for the browser
option.HttpOnly = true;
//// A little non logical way to actually get the HttpResponse (from the HttpRequest and its HttpContext)
req.HttpContext.Response.Cookies.Append("MyCookie", "MyValue", option);
return new OkObjectResult("Cookie set");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment