Federal authorities on Friday arrested a 29-year-old Moroccan man in an alleged plot to carry out a suicide bombing at the U.S. Capitol, the latest in a series of terrorism-related arrests resulting from undercover sting operations.

For more than a year, Amine El Khalifi, of Alexandria, considered attacking targets including a synagogue, an Alexandria building with military offices and a Washington restaurant frequented by military officials, authorities said. When arrested a few blocks from the Capitol around lunchtime on Friday, he was carrying what he believed to be a loaded automatic weapon and a suicide vest ready for detonation.

The gun and vest were provided not by al-Qaeda, as Khalifi had been told, but by undercover FBI agents who rendered them inoperable, authorities said.

They said Khalifi had been the subject of a lengthy investigation and never posed a threat to the public. On Friday afternoon, he made an initial court appearance in U.S. District Court in Alexandria, where he was charged with attempting to use a weapon of mass destruction against federal property. He faces life in prison if convicted.

Khalifi “allegedly believed he was working with al-Qaeda,” said Neil H. MacBride, U.S. attorney for the Eastern District of Virginia. Khalifi “devised the plot, the targets and the methods on his own.”

In several recent terrorism sting operations, critics have accused federal investigators of provoking suspects and, in some cases, suggesting possible targets or tactics. Legal experts say the FBI sometimes walks a fine line in such cases.

“You want to be very sure that the narrative is not substantially provided by the government,” said Karen Greenberg, director of the Center on National Security at Fordham Law School, who studies terrorism sting operations. “There’s a lot of gray area in these cases.”

But officials said Friday that Khalifi, who allegedly conducted surveillance on the Capitol and engaged in methodical planning, was no unwitting victim.

Khalifi arrived in the United States when he was 16 and was living as an illegal immigrant in Northern Virginia, having overstayed his visitor’s visa for years, officials said. In 2010, he was evicted from an Arlington apartment after having failed to pay rent.

The landlord of that apartment, Frank Dynda, a retired patent lawyer, said, “He was getting mysterious packages labeled ‘books,’ but I didn’t think there were books in them.”

Dynda said he thought Khalifi was “suspicious and hostile,” and Dynda reported Khalifi to Arlington police. Two officers visited Dynda’s apartment building soon after the report but told him there was no reason to pursue the matter, he said.

It was unclear how Khalifi came to the attention of federal authorities. According to the criminal complaint filed in court Friday, a confidential source reported to the FBI in January 2011 that Khalifi had met at a residence in Arlington with individuals, one of whom produced what appeared to be an AK-47 assault rifle, two revolvers and ammunition.

When one of the other individuals expressed the sentiment that “the ‘war on terrorism’ was a ‘war on Muslims’ and said that the group needed to be ready for war,” Khalifi reportedly agreed, according to the complaint.

Khalifi “sought to be associated with an armed extremist group” and was introduced on Dec. 1, 2011, to a man called Yusuf, who was an undercover law enforcement officer.

According to the criminal complaint, during meetings with the undercover officer, Khalifi indicated his desire to conduct an operation in which he could carry out a shooting rampage in a restaurant. That restaurant — like the synagogue — was not identified in court documents.

On Jan. 15, Khalifi told undercover agents that he had modified his plans for the attack and wanted to conduct a suicide bombing at the Capitol, according to the complaint. It said that on that same day, at a quarry in West Virginia, Khalifi carried out a test bombing using a cellphone as a detonation device; the test bomb exploded, and Khalifi expressed a desire for a larger explosion in his attack.

On Friday, before preparing for what he allegedly considered a “martyrdom” mission, Khalifi prayed at Dar Al-Hijrah, a Northern Virginia mosque, according to its imam, Johari Abdul-Malik, who said he learned of Khalifi’s presence in an afternoon phone call from the FBI. “They said that the guy prayed at the mosque this morning,” Abdul-Malik said. “They said they’ve been following him for a long time now, and he’s not a regular attender at our mosque nor any other mosque.”

Khalifi was driven into downtown Washington by Yusuf and another man who was working undercover with the FBI. Afterward, Khalifi began walking alone toward the Capitol but quickly was arrested, authorities said.

“There is no doubt that this guy was committed,” said a law enforcement official, speaking on the condition of anonymity to discuss the investigation.

Following the arrest, FBI agents and Arlington police raided a red-brick rambler on Randolph Street in the Douglas Park neighborhood, near a wooded area with trails and a creek. Agents were seen going in and out of the house and searching the back yard. Arlington police said they were assisting with a search warrant.

As news of the arrest spread, several members of the mosque Khalifi visited expressed concern that they could be thrust into the spotlight once again, even though Khalifi was not thought to have been a regular worshiper at the mosque.

Dar Al-Hijrah has weathered repeated criticism for ties to worshipers who were found to have been terrorism suspects. The mosque’s leaders have noted that, as one of the largest mosques in the Mid-Atlantic, it attracts worshipers from all over, including many who attend infrequently.

In the past year, federal agents have arrested at least 20 people in the United States on terrorism-related charges, according to the Senate Intelligence Committee. Washington has been the alleged target in at least two terrorism cases. In one, a Massachusetts man of Bangladeshi descent was arrested for allegedly plotting to fly explosives-packed model planes into the Pentagon and the Capitol. In the other, Farooque Ahmed, a Pakistani American from Ashburn, attempted to bomb Washington area Metro stations. In both of those cases, the FBI relied on undercover agents.

Ashraf Nubani, a Muslim lawyer in Washington who has defended terrorism suspects in similar cases in the past, said he has has watched with alarm the increase of such FBI stings.

“It’s controlled from beginning to end by FBI. But you can’t create a terrorism case and then say you stopped it,” Nubani said. “Had the FBI not been involved, through their manipulation or informants, would the same thing have happened? Would there be attempted violence? They have their sights on certain people, the ones who talk big talk.”

Dean Boyd, a spokesman for the Justice Department, said the affidavit in the Khalifi case makes clear that “at each step, it was the defendant who proposed the alleged plot and sought help in obtaining the weapons to carry it out.”

“Whenever we conduct an undercover operation of this sort, we fully anticipate that allegations of entrapment will be raised as a defense, and we conduct the investigation accordingly to assure that entrapment does not occur,” he said.

Khalifi is due in court Wednesday afternoon for a preliminary hearing.

Staff writers William Branigin, Jason Ukman, Jeremy Borden, Katherine Driessen, Allison Klein, Erica W. Morrison, Dan Morse and Clarence Williams and staff researcher Julie Tate contributed to this report.