Select to view content in your preferred language

Problem adding in ArcGISDynamicMapServiceLayer by code - Log in Dialog

788
3
08-19-2013 06:25 PM
WeePok_Jin
Emerging Contributor
Hi,

I am using the code in this page (http://blogs.esri.com/esri/arcgis/2010/02/16/how-to-use-secure-arcgis-server-services/) to add secured ArcGISDynamicMapServiceLayer into my map.

When I run the code, a sign in dialog box pop up to ask for credentials again, as illustrated in attachment. The layer is added in even I ignore the dialog box by clicking at the "cancel" button.

Does anyone knows what is happening? and how to solve this problem (no dialog box pop up)?

p/s: The code runs well if I add feature layer, meaning that no dialog box pop up.
0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
We would need more info about your code.
The blog post you reference doesn't define any sign in dialog, so it's difficult to help you out about this sign in dialog which is likely defined in your code.
0 Kudos
WeePok_Jin
Emerging Contributor
Here are my code, I am actually aiming to write a behaviour for map to add in layer on the map load:

protected override void OnAttached()
        {
            base.OnAttached();
          
            if (!initTimerExpired)
            {
                initTimer.Tick += (o, e) =>
                {
                    initTimerExpired = true;
                    initTimer.Stop();
                };
                initTimer.Start();
            }

            IDictionary<string, string> qString = HtmlPage.Document.QueryString;
            string strRdCode = "";
            string strMM = "";
            string strYF = "";


            foreach (KeyValuePair<string, string> keyValuePair in qString)
            {

                string key = keyValuePair.Key;

                string value = keyValuePair.Value;

                if (key == "sRdCode")
                {
                    if (value == "tLQKwA16uJLh85XVYVOuZw")
                    {
                        MessageBox.Show("No link provided");
                        closeWindows();
                        return;
                    }
                    else
                    {
                        strRdCode = value;
                    }
                }
                else if (key == "yf")
                {
                    strYF = value;
                    strYF = Decrypt(strYF, sKey);
                }
                else if (key == "mm")
                {
                    strMM = value;
                    strMM = Decrypt(strMM, sKey);
                }
            }

            string sURL = "";

            sURL = "http://10.17.13.218/arcgis/rest/services/RMMS_Label/MapServer";

            loadLayer(sURL, strYF, strMM, "Sarawak Road Label", "Dynamic", strRdCode);
}


private void loadLayer(string sURL, string sYF, string sMM, string sLayerName, string sLyrType, string strRdCode)
        {
           

            if (sLyrType == "Dynamic")
            {             

                ArcGISDynamicMapServiceLayer lyr = new ArcGISDynamicMapServiceLayer()
                {
                Url = sURL,
             
                };
               

                lyr.InitializationFailed += lyr_InitializationFailed;
              
             
                ConfigureLayerWithToken(lyr, sYF, sMM, sLayerName, sLyrType);
              
            }
            else if (sLyrType == "Feature")
            {
                FeatureLayer lyr = new FeatureLayer();
               
                lyr.OutFields.Add("*");
                lyr.Mode = FeatureLayer.QueryMode.OnDemand;

                lyr.Url = sURL;
              
                lyr.InitializationFailed += lyr_InitializationFailed;

                ConfigureFeatureLayerWithToken(lyr, sYF, sMM, sLayerName, sLyrType, strRdCode);
            };         

        }


private void ConfigureLayerWithToken(ArcGISDynamicMapServiceLayer layer, string strYF, string strMM, string strMZ, string sLyrType)
        {
           
            string tokenurl =
                string.Format("http://10.17.13.218/arcgis/tokens/?request=getToken&username={0}&password={1}&timeout={2}",
            strYF, strMM,60);

            WebClient tokenService = new WebClient();

            tokenService.DownloadStringCompleted += (sender, args) =>
            {              
               
                layer.Token = args.Result;
                        

                string originalUrl = layer.Url;

                if (MapApplication.Current.Map.Layers.Contains(layer))
                {
                    layer.Url = null;
                    layer.Url = originalUrl;
                }
                else
                 
                    layer.DisableClientCaching = true;

             
                    MapApplication.Current.Map.Layers.Add(layer);
                 
            };

            tokenService.DownloadStringAsync(new Uri(tokenurl));
        }

        void lyr_InitializationFailed(object sender, EventArgs e)
        {
        }
0 Kudos
WeePok_Jin
Emerging Contributor
Finally I manage to solve my problem by adding this code: IdentityManager.Current.ChallengeMethod = null;

Seems like the SL API still challenge for identity even though I assigned valid token to the added Dynamic Layer.

I do not know whether it is a correct way but at least no more dialog box pop for credential.
0 Kudos