AnsweredAssumed Answered

How do I check whether the current user has access rights to an AF database?

Question asked by manishmakwana on Feb 11, 2019
Latest reply on Feb 12, 2019 by manishmakwana

I'm currently working on a C# .NET MVC application using PI AF SDK, and would like to check whether the current user has access rights to an AF database - so I can present a useful error message either to my app logs or to the user.


I've been trying to troubleshoot a System.NullReferenceException ("Object reference not set to an instance of an object.") I get when trying to access an AF database (specifically, calling "afDatabase.Refresh()" after initialising the database object). It turns out this was due to the user account I was using not having security permissions on the AF database in question.


While I would prefer that I get a more meaningful security related exception, I would like to use an explicit security check in my code instead of relying on exceptions. I note there is an AFSecurity class available that offers promising properties (".CanRead()" etc). However, there are no examples provided to how to actually initialise and use this class. I've googled around and can't find any PI Square threads showing this either.


If I try something like afDatabase.Security.CanRead(); I get an error "AFSecurity does not contain a definition for 'CanRead'..."


Code example:

PISystems piSystems = new PISystems();
PISystem piSystem = piSystems["anAFServer"];
AFDatabase afDatabase = piSystem.Databases["anAFDatabase"];
AFSecurity secObj = afDatabase.Security;
bool userCanReadDB = secObj.CanRead();          // error here


I'd also like to be able to check the security for the AF elements accessed as well.


Any suggestions?