How do I get Imperials to stop attacking me on sight?

Solution 1:

This has a tendency to happen quite a lot. If you are some way into the campaign on either side, then you will be a known member of that faction. In other words; After a certain mission, you will be attacked by the opposite faction.

This will stop after you have completed the missions from that faction, so try to do this as quickly as possible.

As said by John, it could also be because you are wearing the equipment set of the opposite faction, which will also cause them to attack you.

Solution 2:

It's probably not a bug. You are probably dressed as a Stormcloak when you enter the city, or as that guy suggests, you become recognizable as a Stormcloak after progressing past a certain point in the civil war. Dressing as an Imperial will allow you to enter Imperial cities, but you'll then have the same problem in Stormcloak cities unless you change back. There is a Master of Disguise mod that allows subtler effects, tying your Disguise ability to both clothing worn and Stealth skill. I use it, but it now seems to be missing from Nexus. I see at least one other disguise mod still available that may help you complete your current mission, but I haven't tried any other disguise mods, so I can't recommend one.

It's possible, though, that there are other explanations for the behavior, for example severe vampirism or a bounty. Since you didn't provide much information on which cities are definitely affected, how you confirmed, how far along in the civil war you are, or whether you have been turned into a vampire or joined the Companions, it's hard to say for sure what might be happening. Make sure you don't have a bounty in any of the affected cities and that you don't have vampirism. Also, if you still suspect a glitch, try leaving the area and waiting a week before returning.