NEW YORK—Exactly six weeks before Election Day, President Barack Obama stood on the world stage Tuesday and warned Iran that the United States will "do what we must" to stop Tehran from getting a nuclear weapon.

In what could be his last speech to the annual U.N. General Assembly, Obama also told Arab Spring countries groping their way uncertainly toward democracy that they have a friend—and a role model—in America. But, he said, they must battle the forces of intolerance and extremism threatening what should be "a season of progress."

"The United States of America will always stand up for these aspirations, for our own people, and all across the world. That was our founding purpose," he said.

The president, under fire from Republican presidential nominee Mitt Romney for his handling of Iran's atomic ambitions, dedicated part of his 30-minute address to warning the Islamic republic that he cannot live with a nuclear-armed Tehran.

"Make no mistake: a nuclear-armed Iran is not a challenge that can be contained," Obama said.

"It would threaten the elimination of Israel, the security of Gulf nations, and the stability of the global economy. It risks triggering a nuclear-arms race in the region, and the unraveling of the non-proliferation treaty," Obama continued. "That's why the United States will do what we must to prevent Iran from obtaining a nuclear weapon."

The president's stern comments closely echoed his past warnings, and stopped short of drawing the clear "red line" Israeli Prime Minister Benjamin Netanyahu has sought from Washington.

(Romney has at times taken a tougher stance. In a July speech in Jerusalem, he declared that "Iran's pursuit of nuclear weapons capability presents an intolerable threat to Israel, to America, and to the world." The key word there was "capability"—not an actual nuclear weapon, but the ability to build one. That lined the Republican up more closely with Netanyahu.)

Obama denounced an anti-Islam video on the Internet that has partly fueled violent demonstrations throughout the Muslim world, calling the film "crude and disgusting." But he explained that he could not simply ban it—and scolded those who denounce anti-Muslim speech but stay quiet when the target is Christianity.

Story continues

"The future must not belong to those who slander the prophet of Islam. But to be credible, those who condemn that slander must also condemn the hate we see in the images of Jesus Christ that are desecrated, churches are destroyed, or the Holocaust is denied," he said, in an apparent reference to Iranian President Mahmoud Ahmadinejad.

"It is time to marginalize those who, even when not resorting to violence, use hatred of America, or the West, or Israel as the central organizing principle of politics," Obama said. "For that only gives cover, and sometimes makes an excuse, for those who resort to violence."

Obama noted that freedom of speech means he can condemn, but not ban, the video. "As president of our country, and commander-in-chief of our military, I accept that people are going to call me awful things every day," he said, drawing laughter from the audience of dignitaries. "And I will always defend their right to do so." And he invited the Muslim world to draw inspiration from America's protections for freedom of speech and religion.

"We do so because in a diverse society, efforts to restrict speech can become a tool to silence critics, or oppress minorities," he said. "We do so because given the power of faith in our lives, and the passion that religious differences can inflame, the strongest weapon against hateful speech is not repression, it is more speech—the voices of tolerance that rally against bigotry and blasphemy, and lift up the values of understanding and mutual respect."

Obama also paid tribute to the slain U.S. ambassador to Libya, Chris Stevens, killed along with three colleagues in what his administration has designated a terrorist attack on the anniversary of 9/11.

Stevens "embodied the best of America," the president said. "Today, we must reaffirm that our future will be determined by people like Chris Stevens, and not by his killers."

Obama also delivered the kind of vigorous defense of his foreign policy that would not be out of place in his stump speech.

"The war in Iraq is over, American troops have come home. We have begun a transition in Afghanistan, and America and our allies will end our war on schedule in 2014," he said. "Al Qaeda has been weakened and Osama bin Laden is no more."

Images of anti-American riots—and the dramatic assault on the U.S. compound in Benghazi, Libya—have helped degrade Obama's once-imposing advantage over Romney on foreign policy.