3 Replies Latest reply on Apr 10, 2019 10:21 AM by gregor

    Connect to AFSDK using NetworkCredential whit Python

    AlvaroZambrano

      I'm trying to authenticate in the AF SDK using NetworCredentials with python like this.

       

       

       

      I still can not figure out how to pass the credentials from Python to make the connection.

       

       

      Could someone help me with this issue?

       

       

      Thank you.

        • Re: Connect to AFSDK using NetworkCredential whit Python
          gregor

          Hi Alvaro,

           

          You have marked Eugene Lee's answer to your other question as correct, indicating you were able to resolve your issue. Your new question tells me the issue still exists. Looking at the error details you've shared here, it looks to me as if you have disabled Windows Authentication. This is the first issue. If your Python client is in the same domain as your PI Data Archive host and the user you are logged on with to your Python client is a domain user, Windows Authentication should work. We also consider Windows Authentication most secure which is another reason this should be your first choice.

           

          I read somewhere that you have created a Trust but the error indicates that the trust is not working. You really need to look at the incoming connection (PI Message Log) and see which information is passed with the connection attempt. Only the passed information can be used. If you add information to e.g. a trust, no matter if the information is wrong or not, you increase the security but if the information is not included with the connection attempt, the client will not be able to connect - like in your case.

            • Re: Connect to AFSDK using NetworkCredential whit Python
              AlvaroZambrano

              Hi Gregor

               

              Indeed, thanks to the previous answer I had access to the data of the Archive without any problem.

               

               

              Now, I am trying to work with the information stored in the AF. However. I am also having problems whit the authentication procces. I would like to do it through the image method using Python but I do not know how to pass the credentials.

               

              Thanks.

                • Re: Connect to AFSDK using NetworkCredential whit Python
                  gregor

                  Hi Alvaro,

                   

                  I didn't recognize that you are attempting to connect against an AF Server this time. Please accept my apologies!

                  I believe you are aware that connecting against an AF Server requires Windows Security.

                   

                  Now let's look at how to do this in code. Because you are interested in explicitly passing a NetworkCredential, let's first simplify the C# example from the help. I am using the opportunity to include required using statements.

                   

                  using OSIsoft.AF;
                  using System;
                  using System.Net;
                  namespace NetCredo
                  {
                      class Program
                      {
                          static void Main(string[] args)
                          {
                              // Get the known servers table and chose the default PISystem
                              PISystem myPISystem = new PISystems().DefaultPISystem;
                              // Define username, password and domain of the account to be used
                              string username = "mlking";
                              string password = "PasswordsInClearTextIsNoGood!";
                              string domain = "PIDevClub";
                              try
                              {
                                  // Create the NetworkCredential and use it for the connection attempt
                                  NetworkCredential credential = new NetworkCredential(username, password, domain);
                                  myPISystem.Connect(credential);
                                  // Inform about successful connection
                                  Console.WriteLine("'{0}' is connected: {1}", myPISystem.Name, myPISystem.ConnectionInfo.IsConnected);
                              }
                              catch (Exception ex)
                              {
                                  // Inform about the reason for a failed connection attempt
                                  Console.WriteLine(ex.Message);
                              }
                              Console.WriteLine("Done! Press any key to quit .. ");
                              Console.ReadKey();
                          }
                      }
                  }
                  

                   

                  Now, let us translate the C# code into Python:

                   

                  import sys  
                  import clr 
                  # Reference to AF SDK; Import of OSIsoft.AF namespace
                  sys.path.append(r'C:\Program Files (x86)\PIPC\AF\PublicAssemblies\4.0')  
                  clr.AddReference('OSIsoft.AFSDK')
                  from OSIsoft.AF import *
                  # Import of System.Net namespace (required for NetworkCredential)
                  clr.AddReference('System.Net')from System.Net import *
                  # Get the known servers table and chose the default PISystem
                  myPISystem = PISystems().DefaultPISystem
                  # Define username, password and domain of the account to be used
                  username = "mlking"
                  password = "PasswordsInClearTextIsNoGood!"
                  domain = "PIDevClub"
                  try:
                      # Create the NetworkCredential and use it for the connection attempt
                      credential = NetworkCredential(username, password, domain)
                      myPISystem.Connect(credential)
                      # Inform about successful connection
                      print("'{0}' is connected: {1}".format(myPISystem.Name, myPISystem.ConnectionInfo.IsConnected))
                  except Exception as ex:
                      # Inform about the reason for a failed connection attempt
                      print(ex.Message)
                  
                  1 of 1 people found this helpful