The treatment of LGBT intelligence staff was exemplified by the case of pioneering codebreaker Alan Turing, who lost his security clearance after a conviction for gross indecency in 1952 and later took his own life.

A series of Cold War scandals featuring gay men meant homosexuality was linked in many people's minds with espionage and betrayal.

As well as John Vassall, a gay civil servant who was caught in a honeytrap by the KGB, at least two of the Cambridge Five spy ring, Guy Burgess and Anthony Blunt, were gay, while a third, Donald MacLean, was bisexual.

There was also Daily Telegraph Moscow correspondent Jeremy Wolfenden - son of John Wolfenden, who chaired the commission that recommended the legalisation of male homosexual acts - who was photographed by the KGB having sex with a man.

MI6 subsequently attempted to use him as a double agent but he turned to heavy drinking and died in 1965 age 31.

Now MI5 is consistently ranked in the top 10 employers for LGBT staff in the Stonewall Workplace Equality Index over the past five years and was named Employer of the Year at the British LGBT Awards in 2018.

Sir Andrew said: “Diversity is vital for MI5, not just because it's right that we represent the communities we serve, but because we rely on the skills of the most talented people whoever they are, and wherever they may be.

"The accolade from Stonewall is a great acknowledgement of continued progress we have made over recent years in ensuring we draw on the widest possible pool of talent in our vital work."

Sir Andrew, who is stepping down as MI5 director general after seven years, also revealed how he pinned onto his office wall the pictures of the victims of the 2005 terror attacks, terror attacks, pivotal moment for MI5 when four men from the UK detonated devices on the transport system.