In November 1999, Senator John McCain, the Arizona Republican and former prisoner of war in Vietnam who was widely considered a military hero, said that his newly launched presidential campaign had made “a very bad mistake.”

Mr. McCain, who at the time was trailing far behind George W. Bush in the race for their party’s nomination, had produced a campaign ad highlighting his career as a Navy pilot and his reverence for his fellow service members. At one moment, the ad showed Mr. McCain walking solemnly through Arlington National Cemetery.

The Army soon said that Mr. McCain’s campaign had never requested permission to film in the cemetery. Even if it had, an Army spokesman said at the time, the request would have been denied because partisan activity is banned at Army installations. A campaign spokesman said that the clip had come from one of the senator’s periodic visits to the graves of his father and his grandfather.

It was an incident in campaign politics that closely mirrored, before it sharply diverged from, another that took place last week. An Army spokesman said on Thursday that former President Donald J. Trump’s campaign had similarly not been given permission to film in a restricted area of Arlington National Cemetery during Mr. Trump’s visit on Monday — and that it could not have received such permission because it would violate federal law.

The Army issued a rare public rebuke of Trump campaign officials for attacks they directed at a cemetery worker who had tried to stop the filming. (A campaign spokesman had accused the worker of experiencing a “mental health episode.”) The Trump campaign had been made aware that filming for campaign purposes was against Army regulations — and campaign officials had nonetheless plowed forward, even, according to the Army, physically pushing aside the cemetery worker.

Mr. Trump and his campaign published photos and video of the former president laying flowers and flashing a thumbs-up at the graves of Marines killed during the withdrawal from Afghanistan three years ago. Other graves are visible in the footage, including one of a Green Beret who died by suicide. The images are still online.