Add support for cookie configuration

This will fix #4479 and #3511 when it is done. Do not merge.