Exemples de code d’authentification de base

  • Rversion finale: Xanadu
  • Mis à jour 1 août 2024
  • 1 minute de lecture
  • Exemples de code d’authentification de base pour plusieurs langages de programmation et versions.

    Perl et les bibliothèques SOAP ::Lite

    Pour fournir une authentification de base lors de l’utilisation de Perl et des bibliothèques SOAP ::Lite, vous pouvez implémenter la fonction suivante :

    sub SOAP::Transport::HTTP::Client::get_basic_credentials {
       return 'user_name' => 'password';
    }

    C# .NET par rapport à 2005 ou versions antérieures

    Lors de l’utilisation de C# .NET par rapport à 2005 ou une version antérieure, vous pouvez tirer parti de l’objet Informations d’identification. Par exemple :

    System.Net.ICredentials cred = new System.Net.NetworkCredential("user_name", "password");
     
    service.ServiceNow proxy = new service.ServiceNow();
    service.get getService = newservice.get();
    service.getResponse getServiceResponse = new service.getResponse();
     
    try
    {
      proxy.Credentials = cred;
      getService.sys_id = "bf522c350a0a140701972dbf876f1610";
      getServiceResponse = proxy.get(getService);
    catch (Exception ex) { }

    C# .NET PAR RAPPORT À 2008

    Lors de l’utilisation de C# .NET VS 2008, vous pouvez tirer parti de l’objet ClientCredentials. Par exemple :

    Demo_Incident.ServiceNowSoapClient client = new Test08WebService.Demo_Incident.ServiceNowSoapClient();
    client.ClientCredentials.UserName.UserName = "admin";
    client.ClientCredentials.UserName.Password = "admin";

    Ensuite, dans votre fichier app.config , recherchez ce qui suit et remplacez « Aucun » par « Basique » :

    <transport clientCredentialType="None" proxyCredentialType="None" realm="" />

    VB .NET

    Lors de l’utilisation de VB .NET, l’utilisation de l’objet Informations d’identification ressemblerait à ce qui suit :

     Sub Main()
            Dim cred As New System.Net.NetworkCredential("user_name", "password")
     
            Dim proxy As New VB_Democm.incident.ServiceNow
            Dim getIncident As New VB_Democm.incident.get
            Dim getResponse As New VB_Democm.incident.getResponse
     
            proxy.Credentials = cred
     
            getIncident.sys_id = "[your sysID here]"
     
            getResponse = proxy.get(getIncident)
     
        End Sub

    La réponse qui en résulte, lorsque l’authentification de base est activée et qu’aucune information d’identification n’est fournie, ressemble à ceci :

     <html>
     <head>   
     <title>Apache Tomcat/5.0.28 - Error report</title>   
     <style>   <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} 
      H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} 
      H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} 
      BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} 
      B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} 
      P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}
      A {color: black;}
      A.name {color: black;}
      HR {color: #525D76;}-->   
      </style>   
      </head>   
      <body>   
      <h1>HTTP Status 401 -\</h1>   
      <HR size="1" noshade="noshade">   
      <p><b>type</b> Status report</p>   
      <p><b>message</b> <u></u></p>   
      <p><b>description</b> 
      <u>This request requires HTTP authentication ().</u></p>   
      <HR size="1" noshade="noshade">   
      <h3>Apache Tomcat/5.0.28</h3>   
      </body>   
      </html>