Dynamicweb Namespace > ContextManager.Cookies Class : GetCookie Method |
'Declaration
Public Shared Function GetCookie( _ ByVal name As String _ ) As HttpCookie
public static HttpCookie GetCookie( string name )
namespace Dynamicweb.Examples.CSharp { class ContextManagerCookieSample { public void SetCookie() { // *** SetCookie(string, string, DateTime) sample *** // Attempt to set cookie. // Will return null if the cookie is not allowed by the user. System.Web.HttpCookie cookie = Dynamicweb.ContextManager.Cookies.SetCookie("SomeSampleCookie", "Some sample value", System.DateTime.Now.AddDays(1)); if (cookie != null) System.Web.HttpContext.Current.Response.Write("Cookie set successfully!"); else System.Web.HttpContext.Current.Response.Write("Cookie not set successfully!"); // *** SetCookie(HttpCookie) sample *** // Create cookie object System.Web.HttpCookie myCookie = new System.Web.HttpCookie("SomeOtherSampleCookie", "Some other sample value"); myCookie.Expires = System.DateTime.Now.AddDays(1); // Attempt to set cookie. // Will return null if the cookie is not allowed by the user. if (Dynamicweb.ContextManager.Cookies.SetCookie(myCookie) != null) System.Web.HttpContext.Current.Response.Write("Cookie set successfully!"); else System.Web.HttpContext.Current.Response.Write("Cookie not set successfully!"); // *** GetCookieOptInLevel() sample *** // Get the Opt-In Level cookie value // If Opt-In Level has not been set, this will return "Dynamicweb.SystemTools.Context.CookieOptInLevel.Functional" Dynamicweb.SystemTools.Context.CookieOptInLevel level = ContextManager.Cookies.GetCookieOptInLevel(); // *** SetCookieOptInLevel(Dynamicweb.SystemTools.Context.CookieOptInLevel) sample *** // Set Opt-In Level to allow all cookies // Setting the Opt-In will write a cookie containing the current Opt-In Level System.Web.HttpCookie optInLevelCookie = ContextManager.Cookies.SetCookieOptInLevel(Dynamicweb.SystemTools.Context.CookieOptInLevel.All); if (optInLevelCookie != null) { System.Web.HttpContext.Current.Response.Write("Opt-In Level Cookie was set successfully!"); } else { // Something went wrong // HttpContext.Current or HttpContext.Current.Response is not available System.Web.HttpContext.Current.Response.Write("Opt-In Level Cookie was not set successfully!"); } // *** UpdateCookie(string, string, DateTime) sample *** // Attempt to set cookie. Will return null if the cookie is not allowed by the user. Dynamicweb.ContextManager.Cookies.SetCookie("SampleUpdateCookie", "Key1=Value1&Key2=Value2", System.DateTime.Now.AddDays(1)); //Attempt to update the "SomeSampleCookie" cookie System.Web.HttpCookie updatedCookie = Dynamicweb.ContextManager.Cookies.UpdateCookie("SampleUpdateCookie", "Key1=UpdatedValue&Key3=Value3", System.DateTime.Now.AddDays(2)); if (updatedCookie != null) { System.Web.HttpContext.Current.Response.Write("Cookie was updated successfully!"); } // *** UpdateCookie(HttpCookie) sample *** // Create cookie object System.Web.HttpCookie testCookie = new System.Web.HttpCookie("SampleOtherUpdateCookie", "Color=Red"); testCookie.Expires = System.DateTime.Now.AddDays(1); // Attempt to set cookie. // Will return null if the cookie is not allowed by the user. if (Dynamicweb.ContextManager.Cookies.SetCookie(testCookie) != null) { testCookie = new System.Web.HttpCookie("SampleOtherUpdateCookie", "Color=White&Size=36"); testCookie.Expires = System.DateTime.Now.AddDays(2); System.Web.HttpCookie updatedTestCookie = Dynamicweb.ContextManager.Cookies.UpdateCookie(testCookie); if (updatedTestCookie != null) { System.Web.HttpContext.Current.Response.Write("Cookie was updated successfully!"); } } } } }
Public Class ContextManagerCookieSample Public Sub SetCookie() ' *** SetCookie(string, string, DateTime) sample *** ' Attempt to set cookie. ' Will return null if the cookie is not allowed by the user. Dim cookie As System.Web.HttpCookie = ContextManager.Cookies.SetCookie("SomeSampleCookie", "Some sample value", System.DateTime.Now.AddDays(1)) If cookie IsNot Nothing Then System.Web.HttpContext.Current.Response.Write("Cookie set successfully!") Else System.Web.HttpContext.Current.Response.Write("Cookie not set successfully!") End If ' *** SetCookie(HttpCookie) sample *** ' Create cookie object Dim myCookie As System.Web.HttpCookie = New System.Web.HttpCookie("SomeOtherSampleCookie", "Some other sample value") myCookie.Expires = System.DateTime.Now.AddDays(1) ' Attempt to set cookie. ' Will return null if the cookie is not allowed by the user. If ContextManager.Cookies.SetCookie(myCookie) IsNot Nothing Then System.Web.HttpContext.Current.Response.Write("Cookie set successfully!") Else System.Web.HttpContext.Current.Response.Write("Cookie not set successfully!") End If ' *** GetCookieOptInLevel() sample *** ' Get the Opt-In Level cookie value ' If Opt-In Level has not been set, this will return "Dynamicweb.SystemTools.Context.CookieOptInLevel.Functional" Dim level As SystemTools.Context.CookieOptInLevel = ContextManager.Cookies.GetCookieOptInLevel() ' *** SetCookieOptInLevel(Dynamicweb.SystemTools.Context.CookieOptInLevel) sample *** ' Set Opt-In Level to allow all cookies ' Setting the Opt-In will write a cookie containing the current Opt-In Level Dim optInLevelCookie As System.Web.HttpCookie = ContextManager.Cookies.SetCookieOptInLevel(SystemTools.Context.CookieOptInLevel.All) If Not IsNothing(optInLevelCookie) Then System.Web.HttpContext.Current.Response.Write("Opt-In Level Cookie was set successfully!") Else ' Something went wrong ' HttpContext.Current or HttpContext.Current.Response is not available System.Web.HttpContext.Current.Response.Write("Opt-In Level Cookie was not set successfully!") End If ' *** UpdateCookie(string, string, DateTime) sample *** ' Attempt to set cookie. Will return null if the cookie is not allowed by the user. ContextManager.Cookies.SetCookie("SampleUpdateCookie", "Key1=Value1&Key2=Value2", System.DateTime.Now.AddDays(1)) 'Attempt to update the "SomeSampleCookie" cookie Dim updatedCookie As System.Web.HttpCookie = ContextManager.Cookies.UpdateCookie("SampleUpdateCookie", "Key1=UpdatedValue&Key3=Value3", System.DateTime.Now.AddDays(2)) If Not IsNothing(updatedCookie) Then System.Web.HttpContext.Current.Response.Write("Cookie was updated successfully!") End If ' *** UpdateCookie(HttpCookie) sample *** ' Create cookie object Dim testCookie As System.Web.HttpCookie = New System.Web.HttpCookie("SampleOtherUpdateCookie", "Color=Red") testCookie.Expires = System.DateTime.Now.AddDays(1) ' Attempt to set cookie. ' Will return null if the cookie is not allowed by the user. If Not IsNothing(ContextManager.Cookies.SetCookie(testCookie)) Then testCookie = New System.Web.HttpCookie("SampleOtherUpdateCookie", "Color=White&Size=36") testCookie.Expires = System.DateTime.Now.AddDays(2) Dim updatedTestCookie As System.Web.HttpCookie = ContextManager.Cookies.UpdateCookie(testCookie) If Not IsNothing(updatedTestCookie) Then System.Web.HttpContext.Current.Response.Write("Cookie was updated successfully!") End If End If End Sub End Class
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2