WASHINGTON — President Trump had a blunt message for Montana voters last week, an unapologetic reprise of the promise to protect Medicare and Social Security that he used during the 2016 presidential campaign to successfully appeal to older, blue-collar voters.

“They’re going to hurt your Social Security so badly, and they’re killing you on Medicare. Just remember that. I’m going to protect your Social Security,” Mr. Trump said at a campaign rally in Billings on behalf of Matt Rosendale, a Republican Senate candidate.

“Do you remember when I ran?” Mr. Trump asked the cheering crowd, recalling how he had accused the other Republican presidential contenders of wanting to destroy the safety net for older Americans. “I said I’m not touching Social Security. Everybody said — well — and everybody else wanted to do things with Social Security. I said, we’re not touching your Social Security.”

The president’s message once again runs counter to many in his own party, including Speaker Paul D. Ryan, who have long argued for cuts in entitlement programs like Medicare and Social Security. But this time, as Mr. Trump made clear at the Montana rally, his target is not his fellow Republicans, but Democratic candidates in the midterm election contests this fall.