How I fixed SSH X-forwarding error “X11 connection rejected because of wrong authentication”

Problem:

I connected to my server with X forwarding using

ssh -CX user@server

but when I start a graphical application on the server, I see the following error messages:

user@server $ gparted
X11 connection rejected because of wrong authentication.
Unable to init server: Could not connect: Connection refused

(gpartedbin:70357): Gtk-WARNING **: 00:06:19.612: cannot open display: localhost:11.0

Solution:

I found the solution in this StackOverflow post:

sudo --preserve-env=DISPLAY -s
xauth merge /home/user/.Xauthority

Note that you have to replace /home/user with the appropriate home directory.