Create cookie with ASP.NET Core

Create cookie with ASP.NET Core

To create a cookie in an ASP.NET Core application, you can use the Response.Cookies.Append method in a controller action or middleware. Here's an example:

using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; public class HomeController : Controller { public IActionResult SetCookie() { // Create a new cookie with a name of "myCookie" and a value of "myValue" var cookieOptions = new CookieOptions { Expires = DateTimeOffset.Now.AddDays(1) }; Response.Cookies.Append("myCookie", "myValue", cookieOptions); return View(); } } 

In this example, a new CookieOptions object is created to set the expiration date for the cookie. The Response.Cookies.Append method is then called to create a new cookie with a name of "myCookie" and a value of "myValue". The CookieOptions object is passed to the Append method to set the expiration date of the cookie.

You can then retrieve the cookie value in a subsequent request by using the Request.Cookies collection:

public IActionResult GetCookie() { // Get the value of the "myCookie" cookie var myCookie = Request.Cookies["myCookie"]; return View("Index", myCookie); } 

In this example, the Request.Cookies collection is used to retrieve the value of the "myCookie" cookie. The value of the cookie is then passed to a view as a parameter.

Note that cookies can be manipulated by clients, so you should avoid storing sensitive information in cookies. Also, be aware that cookies have a size limit, so you should avoid storing large amounts of data in cookies.

Examples

  1. "C# ASP.NET Core create simple cookie"

    Code Implementation:

    // In a controller action method public IActionResult CreateSimpleCookie() { CookieOptions options = new CookieOptions { Expires = DateTime.Now.AddDays(1) // Set expiration to 1 day from now }; Response.Cookies.Append("MyCookie", "CookieValue", options); return View(); } 

    This code creates a simple cookie named "MyCookie" with the value "CookieValue" in an ASP.NET Core controller action method.

  2. "C# ASP.NET Core create cookie with domain"

    Code Implementation:

    // In a controller action method public IActionResult CreateCookieWithDomain() { CookieOptions options = new CookieOptions { Domain = "example.com" }; Response.Cookies.Append("MyCookie", "CookieValue", options); return View(); } 

    This code creates a cookie with a specified domain, in this case, set to "example.com," using ASP.NET Core.

  3. "C# ASP.NET Core create secure cookie"

    Code Implementation:

    // In a controller action method public IActionResult CreateSecureCookie() { CookieOptions options = new CookieOptions { Secure = true }; Response.Cookies.Append("MySecureCookie", "SecureCookieValue", options); return View(); } 

    This code creates a secure cookie by setting the Secure property to true in an ASP.NET Core controller action method.

  4. "C# ASP.NET Core create cookie with path"

    Code Implementation:

    // In a controller action method public IActionResult CreateCookieWithPath() { CookieOptions options = new CookieOptions { Path = "/subpath" }; Response.Cookies.Append("MyCookie", "CookieValue", options); return View(); } 

    This code creates a cookie with a specified path, in this case, set to "/subpath," using ASP.NET Core.

  5. "C# ASP.NET Core create cookie with expiration"

    Code Implementation:

    // In a controller action method public IActionResult CreateCookieWithExpiration() { CookieOptions options = new CookieOptions { Expires = DateTime.Now.AddDays(7) // Set expiration to 7 days from now }; Response.Cookies.Append("MyCookie", "CookieValue", options); return View(); } 

    This code creates a cookie with a specified expiration date, in this case, set to 7 days from the current date, using ASP.NET Core.

  6. "C# ASP.NET Core create cookie with multiple values"

    Code Implementation:

    // In a controller action method public IActionResult CreateCookieWithMultipleValues() { CookieOptions options = new CookieOptions(); options.Expires = DateTime.Now.AddDays(1); Response.Cookies.Append("MyCookie", "CookieValue1", options); Response.Cookies.Append("MyCookie", "CookieValue2", options); return View(); } 

    This code creates a cookie with multiple values using ASP.NET Core. Note that cookies can have multiple values by appending multiple cookies with the same name.

  7. "C# ASP.NET Core create session cookie"

    Code Implementation:

    // In a controller action method public IActionResult CreateSessionCookie() { CookieOptions options = new CookieOptions { HttpOnly = true // Set HttpOnly to true for session cookies }; Response.Cookies.Append("MySessionCookie", "SessionCookieValue", options); return View(); } 

    This code creates a session cookie with the HttpOnly property set to true for added security using ASP.NET Core.

  8. "C# ASP.NET Core create cookie with same-site policy"

    Code Implementation:

    // In a controller action method public IActionResult CreateCookieWithSameSitePolicy() { CookieOptions options = new CookieOptions { SameSite = SameSiteMode.Strict }; Response.Cookies.Append("MyCookie", "CookieValue", options); return View(); } 

    This code creates a cookie with a specified SameSite policy (e.g., Strict) using ASP.NET Core.

  9. "C# ASP.NET Core read cookie value"

    Code Implementation:

    // In a controller action method public IActionResult ReadCookieValue() { string cookieValue = Request.Cookies["MyCookie"]; // Use the cookie value return View(); } 

    This code reads the value of a cookie named "MyCookie" in an ASP.NET Core controller action method.

  10. "C# ASP.NET Core delete cookie"

    Code Implementation:

    // In a controller action method public IActionResult DeleteCookie() { Response.Cookies.Delete("MyCookie"); return View(); } 

    This code deletes a cookie named "MyCookie" using ASP.NET Core.


More Tags

xss lan fragment-tab-host macos-carbon javapns launchctl pywin32 cryptography progressdialog angular2-router3

More C# Questions

More Biochemistry Calculators

More Date and Time Calculators

More Chemical thermodynamics Calculators

More Animal pregnancy Calculators