Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Cookies - ASP.NET

RSS
Modified on Tue, Jun 05, 2012, 12:18 PM by Administrator Categorized as ASP·NET MVC, ASP·NET Web Forms
public static HttpCookie SetCookie(this HttpResponse response, string name, string value, int expiryDays)
{
    var cookie = response.Cookies[name];

    if (cookie != null)
        response.Cookies.Remove(name);

    cookie = new HttpCookie(name, value);
    cookie.Path = "/";

    if (expiryDays > 0)
        cookie.Expires = DateTime.Now.AddDays(expiryDays);

    response.Cookies.Add(cookie);

    return cookie;
}
public static void DeleteCookie(this HttpResponse response, string name, HttpRequest request)
{
    var c = request.Cookies[name];
    request.Cookies.Remove(name);

    if (c != null)
    {
        c.Expires = DateTime.Now.AddDays(-10);
        c.Value = null;
        response.SetCookie(c);
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.