KABUL — Under elaborate secrecy, President Obama slipped into Afghanistan on Sunday near the front lines of the increasingly bloody eight-year-old war he is expanding and affirmed America’s commitment to destroying al-Qaida and its extremist allies in the land where the Sept. 11, 2001, plot was hatched.

Obama’s six-hour visit was conducted entirely under the shroud of nightfall, after Air Force One’s unannounced flight from the U.S. Obama defended his decision to escalate the fight, telling troops whose numbers he is tripling that their victory is imperative to America’s safety.

His bid to shore up faith in the struggle was aimed both at the troops who cheered him and Americans back home.

As well, he demanded accountability from Afghan authorities to make good on repeated promises to improve living conditions, rein in corruption and enforce the rule of law to prevent people from joining the insurgency.

“Your services are absolutely necessary, absolutely essential to America’s safety and security,” the president told a lively crowd of about 2,500 troops and civilians at Bagram Air Base north of Kabul. “Those folks back home are relying on you. We can’t forget why we’re here.”

It was Obama’s first trip as president to Afghanistan, where the number of U.S. troops killed has roughly doubled in the first three months of 2010 compared with the same period last year as Washington has added tens of thousands more soldiers to reverse the Taliban’s momentum.

“We did not choose this war,” the president reminded the troops, recalling the Sept. 11, 2001, attacks and warning that al-Qaida was still using the region to plan terrorist strikes against the U.S. and its allies. “We are going to disrupt and dismantle, defeat and destroy al-Qaida and its extremist allies.”

Obama arrived in Kabul just two days after a threatening new audio message from al-Qaida leader Osama bin Laden, believed to be hiding along the ungoverned border between Afghanistan and Pakistan.

“I thought I’d come over and say hello,” Obama told the troops, setting the stage for stark reminders of the terrorist threat that rose from this soil.

“If this region slides backwards,” Obama told the troops, “if the Taliban retakes this country, al-Qaida can operate with impunity, then more American lives will be at stake, the Afghan people will lose their opportunity for progress and prosperity and the world will be significantly less secure. As long as I’m your commander in chief, I’m not going to let that happen.”

That resolve was meant just as surely for stateside citizens as for the people who heard it face to face. Polls find that Americans are divided on the war, if, more recently, supportive of Obama’s handling of it.

White House officials said Obama, in private talks, wanted to drive home the point that Afghan President Hamid Karzai and his Cabinet must do more to battle corruption and cronyism in government.

Karzai “needs to be seized with how important that is,” said Jim Jones, Obama’s national security adviser. Karzai has raised eyebrows in Washington with recent trips to Iran, China and Pakistan and his welcoming Iranian President Mahmoud Ahmadinejad to Kabul this month.

In public remarks, Obama told Karzai and his Cabinet that he was pleased with progress made since their last discussion by secure videoconference on March 15. Obama invited him to visit Washington on May 12. He also praised recent steps in the military campaign against insurgents. But he stressed that Afghans need to see conditions on the ground get better.

“We also want to continue to make progress on the civilian front,” Obama said, referring to anti-corruption efforts, good governance and adherence to the rule of law.

Karzai promised that his country “would move forward into the future” to eventually take over its own security.

He told Obama he has begun to establish more credible national institutions on corruption and made clear he intends to make ministerial appointments more representative of the multiple ethnic and geographic regions of the country, according to a U.S. account of the meeting.

Karzai pledged after fraud-marred August elections to rein in graft by making officials declare their assets and giving the country’s anti-corruption watchdog more power to go after those accused of misusing their office.