Shock at the onslaught by the Islamic State of Iraq and the Levant (ISIL) — gobbling up Sunni areas of Iraq and slaughtering Iraqi Shiites — has pushed the United States and Iran to consider a tacit alliance reminiscent of their efforts more than a decade ago in ousting the Taliban in Afghanistan.

The crisis has once again put Washington and Tehran on the same side, opposing Sunni sectarian extremists who hate both Americans and Shia Muslims. But the enemy of an enemy is not necessarily a friend, and Washington faces a tricky challenge in working with Tehran to contain ISIL and preserve what’s left of the Iraqi state while avoiding being drawn onto a widening regional sectarian battleground on which some key U.S. allies are ranged against Iran.

In comparison to what is unfolding now in Iraq and Syria, Afghanistan was easy. After the Sept. 11 attacks, members of the Iranian Revolutionary Guards’ elite Quds Force, working with the Iran-backed Northern Alliance, provided targeting information used by U.S. forces to bomb Taliban positions. The Taliban was tossed out of Kabul in a matter of a few weeks.

In Afghanistan, however, Iran’s cooperation was indirect, and U.S. authorities either didn’t know, or sought to downplay, the Iranian contribution. At the time, U.S. and Iranian diplomats were also meeting discreetly in Europe on the sidelines of United Nations consultations on Afghanistan. But the George W. Bush administration remained leery of open ties with the Islamic Republic, and in his 2002 State of the Union address, President Bush included Iran in his “axis of evil” along with Iraq and North Korea.

The situation now is more conducive to cooperation, when U.S. and Iranian officials communicate with each other directly, routinely and at a high level. After months of insisting that the only topic for these consultations was Iran’s nuclear program, a senior U.S. official disclosed Monday that Iraq would be discussed “on the margins” of previously scheduled talks in Vienna between Iran and the five permanent members of the UN Security Council plus Germany (P5+1). The Iraq talks are likely to be led by U.S. Deputy Secretary of State William Burns, who unexpectedly turned up in Vienna, and Iran’s Foreign Minister Mohammad Javad Zarif.

While Zarif is an extremely well-connected diplomat for the Iranian system, military support for Iraq’s beleaguered Shia is the province of the Quds Force. That unit’s commander, Gen. Qasem Soleimani, has been a target of U.S. sanctions for his role in supporting groups the State Department lists as terrorist organizations – as well as for his alleged part in Iran’s clandestine nuclear activities. So don't look for Soleimani being invited to the White House Situation Room anytime soon.

Another dilemma regarding cooperation, is where and how Iran will direct its support to bolster the government of Prime Minister Nouri al-Maliki. Much of that support is likely to go to Iraqi Shiite militias, some of which have U.S. blood on their hands. Those could include the so-called “League of the Righteous,” which killed five Americans in Karbala in 2007, or others among the "special groups" believed to have blown up Americans with Iranian-supplied improvised explosive devices.

U.S. officials routinely insist that the key to stabilizing Iraq is the creation of a more inclusive government that gives Sunni communities more of a stake in the post-Saddam order. But it’s far from clear whether the U.S. can force Maliki to make even minimal concessions in that direction. Maliki’s sectarian track record after eight years in office offers little grounds for optimism. His regime has arrested, killed or forced into exile most of Iraq’s credible Sunni politicians, and has also alienated the Kurds — the most reliable U.S. allies in Iraq. It’s hard to envision Maliki changing course in the midst of this crisis — or for Iraqi Sunni leaders nursing their grievances in Istanbul or Gulf capitals to respond positively to any overtures from him.

One possibility would be for the U.S. and Iran to agree on Maliki being replaced by a less polarizing figure. Given that Iraq held elections in April in which Maliki’s “State of Law” party won a plurality but not a majority of parliamentary seats, which could be doable. The U.S. and Iran might also seek agreement on a Sunni or Kurdish defense minister — a portfolio Maliki had kept for himself.

Asked Monday if the U.S. was open to military cooperation with Iran in Iraq, Secretary of State John Kerry told interviewer Katie Couric, "I wouldn’t rule out anything that would be constructive to providing real stability, a respect for the constitution, a respect for the election process, and a respect for the ability of the Iraqi people to form a government that represents all of the interests of Iraq, not one sectarian group over another. It has to be inclusive, and that has been one of the great problems of the last few years."

Working through the formal leadership of the Iraqi military, the U.S. can provide more arms, ammunition and training and, if necessary, air support or drone strikes to target ISIL columns. Iran would more likely stick to what it does best — arming Shia militias. Tehran may also move more of its Quds Force advisers from the Syrian front into Iraq to provide the latest intelligence on how to defeat ISIL and its Baathist and tribal allies.

Saudi Arabia and Israel may be appalled by any cooperation with Iran, and some of their supporters may even accuse the U.S. of abandoning them and jumping into bed with Tehran. But even if they do cooperate on Iraq, the U.S. and Iran are unlikely to be walking off into the sunset together any time soon.

The most positive potential result of the Iraq crisis is that it will further incentivize the U.S. and Iran to reach a long-term nuclear agreement. Iran, its resources already depleted by sanctions and the Syrian civil war, urgently needs a nuclear deal. And with so much of the Middle East already in flames, the last thing anyone needs is a confrontation over Iran’s nuclear program.