Loading legend is difficult when using authentication

Discussion created by nijkampr on Jul 26, 2011
Latest reply on Jul 29, 2011 by njarecha-esristaff

We were using the legend example from the website (
This worked like a charm, but when we enabled the authentication on the mapserver (HTTP authentication), it stopped working.

Why? In the statement
AGSMapServiceInfo* msi = ((AGSDynamicMapServiceLayer*)layer).mapServiceInfo;
is the MapServiceInfo allocated, but there are no credentials available! These are not copied from the layer itself, nor is done when the DynamicMapServiceLayer from the mapview is used to retrieve the MapServiceInfo.

Is this wanted? Because it is NOT possible to set the credentials, without recreating the whole object...

The quick and dirty fix i've used now is:
AGSMapServiceInfo* msi = [[AGSMapServiceInfo alloc] initWithURL:self.mapView.dynamicLayer.URL credential:self.mapView.dynamicLayer.credential error:&error];

This works, because there is only one dynamic layer, and only one layer (the dynamic layer) which uses the legend.

It's still work in progress, but this is the code as used:

        NSError* error = nil;
        AGSMapServiceInfo* msi;
        if (((AGSDynamicMapServiceLayer*)layer).credential.authType == AGSAuthenticationTypeNone){
            msi = ((AGSDynamicMapServiceLayer*)layer).mapServiceInfo;
        } else {
            msi = [[[AGSMapServiceInfo alloc] initWithURL:self.mapView.dynamicLayer.URL credential:self.mapView.dynamicLayer.credential error:&error] autorelease];
        if (error)
            NSLog(@"Error encountered while fetching legend : %@",error);
   msi.delegate = self;
   [msi retrieveLegendInfo];
  }else {
   NSLog(@"Skipping layer [%@]. ArcGIS Service must be version 10 SP1 or above",msi.URL );

But, it didn't work in the beginning as expected. The application gave JSON paring errors, which didn't make sense.
After a lot of research, we've found the reason: There was an "#" in the password. The legend is retrieved through http://user:password@url/MapServer/layers?f=json
Because of the # the application couldn't find the used mapserver. I guess the same is applicable for the "/" or "\", and maybe even "&"
The strange part is: With the same authentication, the mapserver itself (The map data) can be loaded...
I guess this last one is a bug... It's rather dirty anyway to put the credentials in an url..

Anyway, i really want to know if my findings are correct, and if it's a bug, or something else.

Many Thanks