Set Cookie - CSharp System.Web

CSharp examples for System.Web:Cookie

Description

Set Cookie

Demo Code


using System.Collections.Specialized;
using System.Web;
using System.Linq;
using System.Configuration;
using System.Collections.Generic;
using System;/*from   w  ww. ja v a  2s.c o m*/

public class Main{
    public static void SetCookie( string name, string value, int lifeInSeconds )
      {
         //Cookie expiry
         DateTime expiry = DateTime.Now;
         expiry = expiry.Add( new TimeSpan( 0, 0, lifeInSeconds ) );

         HttpCookie cookie = new HttpCookie( name );
         cookie.HttpOnly = true;
         cookie.Expires = expiry;

         string domain = AppSettings.Get<string>( "cookieDomain" );
         if( domain.Length != 0 )
         {
            cookie.Domain = domain;
         }
         cookie.Secure = AppSettings.Get<bool>( "cookieSecureOnly" );
         cookie.Shareable = false;
         cookie.Value = value;

         System.Web.HttpContext.Current.Response.Cookies.Add( cookie );
      }
}

Related Tutorials