Providing HTTP\Windows authentication credentials

In general, clients that make Internet connections to ArcGIS Server services use a web service proxy (generated by a WSDL). The proxy works with the web service via SOAP over HTTP. The client does not need to impersonate to work with ArcGIS Server services via an Internet connection. Note that impersonation is merely the ability to control the identity under which client code is executed. Instead, the client process can provide authentication information via the HTTP protocol to the web server\application.

To provide authentication information using a web service proxy, set the credentials on the proxy. Using .NET, you can create a new NetworkCredential, provide the user, password, and domain and assign it to the web service proxy via the Credentials property before calling any methods on the proxy. The example below illustrates how to check whether authentication is enabled on a web service, and if so, provide credential information.

MapService_MapServer mapservice = new MapService_MapServer();

mapservice.Url = "http://localhost:6080/arcgis/services/MapService/MapServer";

 

string url_401 = mapservice.Url + "?wsdl";

HttpWebRequest webRequest_401 = null;

webRequest_401 = (HttpWebRequest)HttpWebRequest.Create(url_401);

webRequest_401.ContentType = "text/xml;charset=\"utf-8\"";

webRequest_401.Method = "GET";

webRequest_401.Accept = "text/xml";

 

HttpWebResponse webResponse_401 = null;

while (webResponse_401 == null || webResponse_401.StatusCode != HttpStatusCode.OK)

{

      try

      {

            webResponse_401 = (HttpWebResponse)webRequest_401.GetResponse();

      }

      catch (System.Net.WebException webex)

      {

            HttpWebResponse webexResponse = (HttpWebResponse)webex.Response;

            if (webexResponse.StatusCode == HttpStatusCode.Unauthorized)

            {

                  if (webRequest_401.Credentials == null)

                  {

                        webRequest_401 = (HttpWebRequest)HttpWebRequest.Create(url_401);

                        webRequest_401.ContentType = "text/xml;charset=\"utf-8\"";

                        webRequest_401.Method = "GET";

                        webRequest_401.Accept = "text/xml";

                        webRequest_401.Credentials = new NetworkCredential("user","password");

                  }

                  else

                  {

                        // if original credentials not accepted, return

                        return;

                  }

            }                 else {

 

                  // if web service unavailable, StatusCode = NotFound, return

                  return;

            }

      } catch (Exception ex) { }

}

 

if (webResponse_401 != null)

      webResponse_401.Close();

 

if (webRequest_401.Credentials != null)

      mapservice.Credentials = webRequest_401.Credentials;

 

// If proper credentials provided, default map name should be returned

string mapname = mapservice.GetDefaultMapName();

11/8/2016