Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to PI server using credentials #511

Closed
gygabyte017 opened this issue Mar 2, 2020 · 14 comments
Closed

Connect to PI server using credentials #511

gygabyte017 opened this issue Mar 2, 2020 · 14 comments

Comments

@gygabyte017
Copy link

Hi, I didn't get how to explicitly pass username and password when connecting to a specific PI server. In my environment I need to provide a technical user to connect to the server instead of using my own Windows credentials.

@Hugovdberg
Copy link
Owner

Currently this isn't supported in the python API, and I think I cannot test it locally. Would you be able to test it if I added it to the code?

@gygabyte017
Copy link
Author

gygabyte017 commented Mar 3, 2020 via email

@Hugovdberg
Copy link
Owner

Can you install the test version using pip install git+https://github.com/Hugovdberg/PIconnect@feature/511_connection_credentials?

You should then be able to connect to the PI Data Archive using something like the following:

import PIconnect as PI
from PIconnect.PIConsts import AuthenticationMode

with PI.PIServer(username='...', password='...', authentication_mode=AuthenticationMode.PI_USER_AUTHENTICATION) as server:
    points = server.search('*')
    print(len(points))

Note that it's required to provide both a username and password (no default username). If you need to provide a login domain you can use the argument domain. If you need Windows authentication instead of PI user authentication you should set the authentication_mode to AuthenticationMode.WINDOWS_AUTHENTICATION.

If you experience any issues please let me know!

@gygabyte017
Copy link
Author

Thank you! Here's my results.

SecureString object seems not to be working: I get PIAuthenticationException.

Something seems wrong because if I print the SecurePassword.Length I get 0, so I guess no password is being passed to the server:

    def __enter__(self):
        if self._credentials:
            print(self.server_name)
            print(self._credentials[0].SecurePassword.Length)
            self.connection.Connect(*self._credentials)
            print(self.connection.CurrentUserName)

Instead, if I replace the SecureString with just a String:

cred = [username, password] + ([domain] if domain else [])

the connection is successful (I added some print to the __enter__ method to get sure that the correct server and the correct user is will logged in).

@gygabyte017
Copy link
Author

This works with SecureString:

        if username:
            from System.Net import NetworkCredential
            from System.Security import SecureString
            
            secureString = SecureString()
            for c in password:
                secureString.AppendChar(c)

            cred = [username, secureString] + ([domain] if domain else [])
            self._credentials = (NetworkCredential(*cred), int(authentication_mode))

I guess a SecureString in .NET cannot be constructed from a string, but must be built char by char...

@Hugovdberg
Copy link
Owner

Thanks for testing! I will fix the SecureString construction, that has some more guarantees the text is removed from memory as soon as possible.

@Hugovdberg
Copy link
Owner

I pushed a new version that incorporates the correct construction of the SecureString, could you please update using the same command I previously sent?

@gygabyte017
Copy link
Author

It works great! I tested with both AuthenticationModes and different domains/credentials, it works in every test.

I believe the issue is solved and can be committed to master, if you agree this can be closed.

Thank you again,
Regards

@Hugovdberg
Copy link
Owner

Great to hear that! I will include it in the development release for now, it will trickle into master in the next release.

@panshu77
Copy link

thanks for adding this new function. Just wonder when the new release with this function will come out. And if not soon, is the test version still available? thanks.

@fouadsel88
Copy link

fouadsel88 commented Jan 28, 2021

Thank you for fixing that, is the new release coming soon? I am getting the same issue and I would like to use AuthenticationMode.WINDOWS_AUTHENTICATION to my script.

@Hugovdberg
Copy link
Owner

you can always install from the github development branch, but I'll try to make a new release soon.

To install the latest developments you can install using: pip install --upgrade git+https://github.com/Hugovdberg/PIconnect@develop

@mhernandez1005
Copy link

Hello, is it possible to connect to PI database over VPN? I receive PIAuthenticationException when trying to connect to pi database. The PIconnect version is 0.9.1, with the updated secure string. Thank you,

@Hugovdberg
Copy link
Owner

Can you connect using official PI software over that VPN? If the SDK supports it, it should be possible using PIconnect as well, but I don't have a VPN to test with, nor a PI server that requires manual authentication. What is the exact error message you get?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants