0

I am trying to add a secure layer of an ArcGIS Server. But even with the user / password, ArcGIS Pro shows me a login window to add my layer.

How to get there directly without this login window?

My code information:

            CIMInternetServerConnection isc = new CIMInternetServerConnection()
            {
                URL = urlFeature,
                User = "username",
                Password = "password",
                Anonymous = false,
                HideUserProperty = false
            };
            CIMAGSServiceConnection ags = new CIMAGSServiceConnection()
            {
                ServerConnection = isc,
                ObjectName = "Transactions",
                Capabilities = "Query,Create,Update,Delete,Uploads,Editing",
                Description = "",
                ObjectType = "Feature Layer",
                URL = urlFeature
            };
            try
            {
                var layer = LayerFactory.Instance.CreateFeatureLayer(ags, map);
            }
            catch (Exception e)
            {
                MessageBox.Show( e.Message);
            }

1 Answer 1

1

Sorry..

I found the response:

In my ArcGIS pro addin,

  1. Create token for ArcGIS Service.
    var url = "https://{baseUrl}/arcgis/tokens/generateToken";
    var request = (HttpWebRequest)WebRequest.Create(url);
    var postData = "f=JSON";
    postData += "&username={username}";
    postData += "&password={password}";
    postData += "&client=ip";
    postData += "&ip=" + GetLocalIP();
    postData += "&expiration=60";
    var data = Encoding.ASCII.GetBytes(postData);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;
    using (var stream = request.GetRequestStream())
    {
      stream.Write(data, 0, data.Length);
    }
    var response = (HttpWebResponse)request.GetResponse();
    var resString = new StreamReader(response.GetResponseStream()).ReadToEnd();
    return (EsriToken)new JavaScriptSerializer().Deserialize(resString, typeof(EsriToken));

EsriToken is a class with 2 properties:

    public class EsriToken
    {
      public string token { get; set; }
      public string expire { get; set; }
    }
  1. Set token in Application cookie for Service url:
    ProApp.SetCookie(new Uri("https://{baseUrl}/arcgis/services/"), "agstoken=" + token); // token is into EsriToken class...
  1. Add the featureLayer as secured ArcGIS Service
    await QueuedTask.Run(() =>
    {
      map = MapFactory.Instance.CreateMap(title, MapType.Map, MapViewingMode.Map, Basemap.ProjectDefault);
      ServiceConnectionProperties serviceConnectionProperties = new ServiceConnectionProperties(new Uri("https://{baseUrl}/arcgis/services/{YourServicePath}"));
      using (Geodatabase fs_db = new Geodatabase(serviceConnectionProperties))
      {
          IReadOnlyList<TableDefinition> definitions = fs_db.GetDefinitions<FeatureClassDefinition>();
          var dataset_name = "{YourDatasetNameHere}";
          var fc = fs_db.OpenDataset<FeatureClass>(dataset_name);
          try
          {
              var layer = LayerFactory.Instance.CreateFeatureLayer(fc, map);
          }
          catch (Exception e)
          {
              throw;
          }
      }
    });
    App.Panes.CreateMapPaneAsync(map);

Tadaa... Et voilà !!

Sorry for my English. I'm working on it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.