Workflow - Cookie (using code) - Web App

Published on Friday, 5 June 2015

In this article I will show you how to create and retrieve Cookies in the App.pngWeb App Project Type

The Cookie components aren't available in the Web App Project type.

In a Forms (Web) Project they are under

Components.png Input Output

Components.png Web Cookies

Clear Cookie.png Clear Cookie

Get From Cookie.png Get From Cookie

Save To Cookie.png Save To Cookie

Class: LogicBase.Components.Default.IO.ClearCookie
Library: LogicBase.Components.Default.dll
Publisher: Symantec Corporation

Some of the Default.IO is available but not these.

The Dialog Model type is not the same as the Forms (Web).png Forms (Web) Project Type in a Web Application Project.

Since we can't use the components we will have to use another method.

Let's go to the trusty Scripting Component.

Add in the Library (LogicBase.Components.Scripting.dll) to your Project

Find the Code (Script) Component.png Code (Script) Component

Set Cookie

We will create the 'Set' Component first, this will have 3 parameters:

CookieName - Text

CookieValue - Text

ExpiresMinutes - Number (integer)

No return type

In the namespaces add

System.Web

//Cookie Class
//
https://msdn.microsoft.com/en-us/library/system.web.httpcookie(v=vs.71).aspx

System.Object
System.Web.HttpCookie
Namespace: System.Web

C# code:

    HttpCookie myCookie = new HttpCookie(CookieName);
    DateTime now = DateTime.Now;
    
    // Set the cookie value.
    myCookie.Value = CookieValue;
    // Set the cookie expiration date.
    myCookie.Expires = now.AddMinutes(ExpiresMinutes);
    
    // Add the cookie.
    HttpContext.Current.Response.Cookies.Add(myCookie);

If you read the MS Articles below you won't see HttpContext.Current.

This is needed for it to work in Workflow.

Get Cookie

For the Get Cookie we need an input of

CookieName - Text

  • 'Result variable type' of Text
  • 'Result variable name' of Cookie

Again add the namespace of System.Web

Then the following C# code:

HttpCookie myCookie = new HttpCookie(CookieName);
myCookie = HttpContext.Current.Request.Cookies[CookieName];

return myCookie.Value;

And now you can use this throughout your project.


To view the Cookies in IE.

F12 - Cache | View Cookie Information


Links

Connect Forum

https://www-secure.symantec.com/connect/forums/need-tutorial-save-cookie-component-usage

MS

// Code: Writing a Cookie (Visual C#)
https://msdn.microsoft.com/en-us/library/aa287547%28v=vs.71%29.aspx

// Code: Reading a Cookie (Visual C#)
https://msdn.microsoft.com/en-us/library/aa287533(v=vs.71).aspx

// HttpCookie Properties
https://msdn.microsoft.com/en-us/library/System.Web.HttpCookie_properties(v=vs.110).aspx


Protirus.png