Very good...it's always good to see somebody devising their own solutions.
Regarding the Legend dijit, this is a bug in ESRI's code that I've encountered and fixed on my locally hosted copy of the API (currently v3.12). For what it's worth, in case it helps anybody else, here are my records of what I did:
esri\dijit\Legend.js - updated so token doesn't occur twice in query string
-updated second line of the _legendRequestServer function to:
(c=a._getToken())&&(b+=((b.indexOf("?")==-1)?"?token\x3d"+c:((b.indexOf("token=")==-1)?"&token\x3d"+c:"")));
-original code was
(c=a._getToken())&&(b+="?token\x3d"+c);
-also updated similar line in _buildRow_Tools function:
(d=c._getToken())&&(g+=((g.indexOf("?")==-1)?"?token\x3d"+d:((g.indexOf("token=")==-1)?"&token\x3d"+d:"")))
-original code was (d=c._getToken())&&(g+="?token\x3d"+d)
-Note - beware not to delete/overwrite the extra right parenthesis at the end of this line