Kentico K# macros cannot retrieve session values (that wouldn't be very safe), but you can create your own macro to return a session value.
An example of the macro class:
using System;
using CMS.MacroEngine;
using CMS.Helpers;
using CMS;
// Makes all methods in the 'SessionMacroContainer' container class available for string objects
[assembly: RegisterExtension(typeof(SessionMacroContainer), typeof(string))]
// Registers methods from the 'CustomMacroMethods' container into the "String" macro namespace
[assembly: RegisterExtension(typeof(SessionMacroContainer), typeof(StringNamespace))]
public class SessionMacroContainer : MacroMethodContainer
{
[MacroMethod(typeof(string), "Gets session value", 1)]
[MacroMethodParam(1, "value", typeof(string), "SessionName")]
public static string GetSessionValue(EvaluationContext context, params object[] parameters)
{
switch (parameters.Length)
{
case 1:
var sessionName = ValidationHelper.GetString(parameters[0], null);
return ValidationHelper.GetString(SessionHelper.GetValue(sessionName), String.Empty);
default:
// No other overloads are supported
throw new NotSupportedException();
}
}
}
In ideal case you should also use your own Namespace instead of the "String" namespace that I'm using for simplicity. Then you could also hardcode the session name so that users cannot access sessions they shouldn't have access to (if necessary).
To use this code simply create class in your App_Code folder or Old_App_Code/class library in case of web apps.
You can call it like:
{% String.GetSessionValue("MySession") |(identity)GlobalAdministrator%}