"You will succeed in your mission," Obama told more than 3,500 cheering troops in a huge hangar. "We said we were going to break the Taliban's momentum. That's what you're doing. You're going on the offense, tired of playing defense."
Obama had traveled to Afghanistan to thank the troops and to deal with frayed relations with Afghan President Hamid Karzai.
But plans for a face-to-face meeting with Karzai were abruptly scrapped. The White House and Afghan officials blamed the last-minute cancellation on rough weather. Instead, the two leaders spoke by phone, Obama at the air base and Karzai in Kabul.
Obama's surprise visit, his second to Afghanistan as president, came a year after he widened the ever deadlier war and ahead of the completion later this month of a review of the 9-year-plus conflict.
"I don't need to tell you this is a tough fight," Obama said. He met with the top NATO commander in Afghanistan, U.S. Gen. David Petraeus, and U.S. Ambassador Karl Eikenberry, and also visited wounded soldiers. He presented five Purple Hearts, military awards for wounded service members.
There are now about 150,000 coalition forces in Afghanistan, roughly 100,000 of them Americans. The U.S. and its NATO partners agreed last month in Lisbon, Portugal, to begin turning over control to local Afghan authorities in 2011, with a goal of completing that transition by the end of 2014.
"We look forward to a new phase next year, the beginning of transition to Afghan responsibility," Obama said.
"Thanks to your service we are making important progress," he told the troops.
"On behalf of more than 300 million Americans, we are here to say thank you ... for everything that you do."
"We will never let this country serve as a safe haven for terrorists who will attack the United States of America again. That will never happen," he said.