Take a look at the Magento\Framework\Session\SidResolver
. You can add it in DI and get current SID using the method getSid
.
You can debug why your session id parameter is mised from the url inside the Magento\Framework\Url
class in the useSessionIdForUrl
method, where the magento checks is SID should be added to the url or not:
/**
* Check and return use SID for URL
*
* @param bool $secure
* @return bool
*/
public function useSessionIdForUrl($secure = false)
{
$key = 'use_session_id_for_url_' . (int)$secure;
if (is_null($this->getData($key))) {
$httpHost = $this->_request->getHttpHost();
$urlHost = parse_url($this->_getScope()->getBaseUrl(UrlInterface::URL_TYPE_LINK, $secure), PHP_URL_HOST);
if ($httpHost != $urlHost) {
$this->setData($key, true);
} else {
$this->setData($key, false);
}
}
return $this->getData($key);
}
SID appears in a request only when you go from one host to other host.