Updated

With striking optimism, Chuck Hagel said Saturday on his final visit to Afghanistan as U.S. defense secretary that he believes Afghans will successfully put down a surge in Taliban attacks in the capital and stabilize the nation.

Hagel arrived in Kabul on a previously unannounced trip one day after President Barack Obama declared that he would nominate one of Hagel's former deputies, Ashton Carter, to succeed Hagel, who resigned under pressure Nov. 24.

In an interview with reporters traveling with him from Washington aboard a military aircraft, Hagel was in a reflective mood about America's longest war. He recalled arriving in Kabul in January 2002 as a member of a congressional delegation when security was so dicey that the lawmakers arrived under cover of darkness and left before dawn. Hagel at the time was a Republican senator from Nebraska.

The Taliban, which had ruled Afghanistan since 1996, were forced from power in late 2001 just weeks after a U.S.-led invasion prompted by the 9/11 terrorist attacks. But they recovered gradually after the U.S. shifted its military focus to Iraq in 2003, and by 2008 the U.S. was conceding that the war in Afghanistan was stalemated.

Hagel, on his fourth trip to Afghanistan as defense secretary, said it should not be surprising that the Taliban are still able to launch significant attacks in the capital.

"The Taliban are going to continue to have pockets of resurgence, and it's predictable that they would do everything they possibly could to disrupt" the new Afghan government under President Ashraf Ghani and Chief Executive Officer Abdullah Abdullah, Hagel said.

The Taliban clearly are aiming to disrupt the new government and undermine confidence in it. But Hagel said they have failed thus far and are unlikely to ever succeed.

"I have confidence in the Afghan security forces that they will continue to meet these challenges," he said.

Hagel said that during his visit he intended to discuss with Ghani and Abdullah the state of security in Kabul and ways that it can be improved. He said the main reason for his visit is to reinforce support for their new government, reiterate a long-term U.S. commitment here and to thank American troops.

The U.S.-led war in Afghanistan has lasted far longer than anyone predicted at the outset in October 2001, and the final result, after more than 2,200 U.S. deaths, remains in doubt even as Obama officially ends the U.S. combat mission Dec. 31.

Hagel said that in hindsight the struggle to prevent Afghanistan from reverting to a haven for al-Qaida has been difficult but worthwhile.

"As difficult, as challenging, as long as this has been, by any definition the country of Afghanistan, the people of Afghanistan, are far better off today than they were 13 years ago, if for no other reason than they have the opportunity to decide their own fate, their own way, on their terms," he said.

"They're not completely there yet, but they've come a long way and that's to the credit certainly of the United States," Hagel said.

At the peak in 2010-2011 the U.S. had 100,000 troops in Afghanistan. The Americans have fought alongside troops from a coalition of countries, including Britain, Canada, Australia, Italy, Denmark, Turkey and Poland.

About 9,800 U.S. troops are to remain after this year as part of a NATO mission, dubbed Resolute Support, to train and assist Afghan security forces and to carry out counter-terrorism missions. The U.S. troop total is to shrink further to 5,500 by the end of next year but the pace of the decline is yet to be decided.

Hagel said he would be consulting with the top U.S. commander here, Army Gen. John Campbell, about the troop drawdown and other issues.