For decades, Bernie Sanders’ independence served him well as he won election after election in Vermont, first to become mayor of the state’s largest city and then to become the longest-serving independent member of Congress in American history. But now, as he tries to win the first real partisan primary of his life, his complicated relationship with the Democratic Party is coming back to haunt him.

That 35-year independent streak publicly came to an end Thursday at the desk of the New Hampshire secretary of state as Sanders filed his paperwork to compete in the state’s Democratic presidential primary. “I’m a Democrat,” he said. “I don’t think I need to say too much more.”

RELATED: Sanders: ‘Categorically false’ that I worked against Obama

If only it were that easy. The problem is that Sanders has a long record of trashing the party whose standard he hopes to bear in next year’s general election — and his opponents are already eager to remind voters of that.

At an MSNBC Democratic forum in South Carolina Friday, rival Martin O’Malley accused Sanders of “trying to find someone to primary” President Barack Obama in 2012. “I’m a lifelong Democrat. I’m not a former independent,” the former Maryland governor added.

Close video Sanders fires up living wage supporters: ‘Enough is enough!’ Democratic presidential candidate and Vermont Senator Bernie Sanders delivers a passionate speech at a “Fight for $15 and a Union” rally on Capitol Hill, demanding higher wages and a universal right to organize a union. Democratic presidential candidate and Vermont Senator Bernie Sanders delivers a passionate speech at a “Fight for $15 and a Union” rally on Capitol Hill, demanding higher wages and a universal right to organize a union. share tweet email Embed

Sanders called the charge that he worked against Obama’s reelection “categorically false” in an interview on ABC’s “This Week” on Sunday, saying he “worked very hard” for Obama.

Indeed, after fighting public battles with the White House over Social Security and taxes, Sanders did embrace the president as that election neared. “I am extremely proud to be here today to say that we are all together going to do everything we can to reelect Barack Obama as president of the United States,” Sanders said in March of 2012 while introducing the president at the University of Vermont.

Sanders also made at least seven trips to New Hampshire to stump for Obama in September of that year.

But Ben LaBolt, who was the Obama campaign’s press secretary in 2012, told MSNBC he remembered things differently.

“Most Democrats — from President Clinton to Governor O’Malley — were all in to support President Obama’s tough reelection campaign,” he said. “Others were promoting the idea of a primary challenge, which would have really handcuffed our ability to fire up the base in the run-up to the general election. Senator Sanders fell into the latter camp.”

Sanders on several occasions said he saw value in a primary challenge to Obama’s left, and four unnamed Obama aides told BuzzFeed News that Sanders was not helpful in the reelection.

This year, Sanders has made a top campaign surrogate out of provocative Princeton Professor Cornel West, who famously called Obama “a black puppet of corporate plutocrats” and more in the run-up to the 2012 election (though he did in the end support the president).

The Central Iowa Democrats’ Fall Barbecue on Sunday will feature Hillary Clinton, Martin O’Malley — and Cornel West appearing on Sanders’ behalf.

For O’Malley, this is a fruitful line of attack. “Senator Sanders cannot re-write history when the reality is that he owes Democrats an explanation, not a denial, as to why he didn’t have the President’s back in 2012,” O’Malley spokesperson Haley Morris told MSNBC.

RELATED: Clinton nabs backing from deep-pocketed environmental group

Hillary Clinton, asked about Sanders’ party affiliation on Monday, was more gentle, but her message was clear. “I’m just proud to be a Democrat and I’m proud that I’ve worked so hard for the Democratic Party,” she said.

Obama remains extremely popular with the party’s base, with a 91% approval rating among liberal Democrats, according to Gallup.

And Clinton, O’Malley, and their outside allies have a deep well to draw from when looking to raise doubts about the Sanders commitment to the Democratic Party. It wouldn’t be the first time his independence has a proved a challenge.

When Sanders ran for Congress in 1990, Democratic leaders pushed him to run on their ticket, but he said that would be “hypocritical” given his past criticism of the party, according to a Rutland Herald article at the time. When he got to Congress, some moderate Democrats objected to the self-described democratic socialist caucusing with their party. They distributed nasty things Sanders had said about their party in an effort to keep him out, which was only partially successful.

In 1984, then-mayor Sanders “participated in a formal Democratic Party function for the first and last time time in my life” to attend a rally for Jesse Jackson’s presidential campaign, he recalled in his autobiography, “Outsider in the House.” When Sanders rose to speak in support of Jackson, “a number of old-line Democrats staged a silent protest by standing up and turning around as I delivered my speech,” he wrote. “When I returned to my seat, a woman in the audience slapped me across the face.”

(The “last” claim would later prove premature. Sanders attended the 2008 Democratic National Convention, but acknowledged at a Democratic National Committee event in Minneapolis in August that he had attended a few other party functions before running for president.)

In a 1989 op-ed for The Burlington Free Press, Sanders said both parties are “intellectually and morally bankrupt.” And the same paper quoted him a year later saying, “Why should we work within the Democratic Party if we don’t agree with anything the Democratic Party says?” And there is much more — just a Lexis/Nexis search away.

Former Vermont Gov. Howard Dean, who is backing Clinton, refused to say the words “Bernie Sanders is a Democrat” while appearing on MSNBC Friday, saying he knew Sanders’ history with the party in the state.

But Sanders’ independence is not just about the Democratic Party. He is simply not a joiner and has, instead, been successful building a movement around himself rather than through a party.

RELATED: At Democratic forum, Bernie Sanders shows his softer side

He left the fringe Liberty Union Party after running on the party’s ticket multiple times in the 1970s and didn’t win an election until he became a solo act.

And while they claim him on their website, Sanders is not even a member of the Vermont Progressive Party, which grew directly out of Sanders’ mayoral campaign, according to a spokesperson. The party is arguably most successful third party in the country, with nine members currently in the Vermont legislature, 36 current and former members of the Burlington City Council, and two Burlington mayors who carried on Sanders’ legacy.

The Vermont Democratic Party also claims Sanders. They don’t recruit candidates to run against him, and work with his campaign team to integrate operations. The party endorsed Sanders’ Senate bids in both 2006 and 2012 — he rejected them both times. “While Senator Sanders has not previously accepted the Party’s endorsement, we believe his positions are consistent with our platform and have always supported his candidacy,” Connor Casey, the party’s executive director, told MSNBC.

Allies say it’s precisely his independence that has led Sanders to draw support from a wide swath of Vermonters and win his last election with a whopping 71% of the vote in 2012. (Last year, Democratic Gov. Peter Shumlin barely won reelection after getting less than 50% of the vote. In 2006, when Sanders was first elected to Senate with 65% of the vote, the state easily re-elected a Republican governor.)

And at a time when voters in both parties are fed up with Washington and the status quo, many may disregard or even like Sanders’ past criticisms of the party. But it could also turn off the party’s hardcore activists, who are so crucial to winning a primary.