Gordon Brown has said the UK must not be "deterred" from its mission in Afghanistan despite the risk to troops, paying tribute to them as "our heroes".
Gordon Brown has told Afghan President Hamid Karzai he will not put UK troops "in harm's way for a government that does not stand up against corruption".
In a speech in London, he said we "cannot, must not and will not walk away" from the mission in Afghanistan.
The UK prime minister said the Afghan government had become a "by-word for corruption" and President Karzai had to take action against it.
His comments came after five soldiers were killed in Helmand on Tuesday by a policeman being trained by UK forces.
Mr Brown proposed a new anti-corruption commission to investigate abuses.
The mentoring must continue "because it is what distinguishes a liberating army from an army of occupation", he added.
"Cronies and warlords should have no place in the future of Afghanistan," said Mr Brown in a keynote speech.
"We will succeed or fail together and we will succeed," Mr Brown stressed.
He also gave Afghan President Karzai an ultimatum over the future conduct of government, saying a new anti-corruption law and anti-corruption commission should be established to stamp out cronyism.
His speech came amid growing unease at Westminster about the situation in Afghanistan, with a handful of Labour MPs calling for a phased withdrawal of British troops.
'Sustained pressure'
The UK's most senior general in Afghanistan, Jim Dutton, has told the BBC the killing of the five soldiers, in Helmand province on Tuesday, "probably won't be the last" such "atrocity".
In his speech, Mr Brown said the UK's role in Afghanistan could not be undertaken without "risk or danger" and paid tribute to the sacrifice of British troops, saying they were "a defining feature" of the British nation.
He defended the "vital" role that British troops were playing in training Afghan troops, saying it was not the "safest or easiest alternative" but it was the "right thing to do".
He said the main terrorist threat to the UK continues to emanate from Afghanistan and Pakistan and that anyone who questioned why UK troops were in Afghanistan should reflect on the terrorist atrocities since 2001.
He added that so long as military action has a "suppressive effect on al-Qaeda", which he says continues to plot attacks on Britain from the region, "we cannot, must not and will not walk away".
Mr Brown added: "We will not be deterred, dissuaded or diverted from taking whatever measures are necessary to protect our security...
The vast majority of those policemen are doing a good job for the future of Afghanistan - as good as they are able to. There is always going to be the odd one or two rogues General Jim Dutton Troop deaths 'not last atrocity'In full: Brown Afghan speech
"We will not give up this strategy of mentoring [police and soldiers], because it is what distinguishes a liberating army from an army of occupation."
The prime minister said the Afghan army should grow from its current strength of 80,000 men to more than 130,000 by the end of 2010.
He predicated that the "heroism" of personnel currently serving in Afghanistan will be taught to future generations "just as in the past we learned of the bravery and sacrifice of British soldiers in the First and Second World Wars".
Mr Brown also made his strongest criticism yet of Afghan President Hamid Karzai, recently re-elected to a second term in office, saying his government had become a "by-word for corruption".
Setting five tests for the Karzai government, Mr Brown said he was no longer willing to put British soldiers in "harm's way" to help protect the country unless the Afghan government was prepared to meet its obligations to its own people.
He called for action to improve governance, root out corruption, support economic development and tackle the drugs trade.
On President Karzai, he said: "He needs a contract with the Afghan people; a contract against which Afghans, as well as the international community, can judge his success."
Mr Brown also called for an "inclusive" political settlement after the recent heavily criticised election and said that all those who rejected violence must be embraced as part of a process of reconciliation.
'Tough questions'
The death of another soldier in Sangin, central Helmand, on Thursday, brings the number of UK personnel killed in Afghanistan since the mission began in October 2001 to 230.
The Lib Dems have said a comprehensive change in strategy is required to make headway in Afghanistan.
The party has denied it is "flirting" with calling for troop withdrawal, stressing it continues to support the mission but defending its right to criticism the course of the war against the Taliban.
"We do understand the costs of withdrawal would be absolutely huge," foreign affairs spokesman Ed Davey told the BBC.
But he added: "It is the opposition's job to ask tough questions when British soldiers are being killed at this appalling rate. It is right to question the strategy. It is failing at an accelerating rate."
The House of Lords is to debate the condition of the armed forces later on Friday.
During the defence debate in the House of Lords, peers including former Chiefs of the Defence Staff Lord Boyce and Lord Craig of Radley are expected to speak.