User management and security
The ITcUserManagement interface allows you to select a user database, log in a user, and retrieve the currently active user. The interface is obtained via dte.GetObject("TcUserManagement") and is available even before a TwinCAT project is opened.
ITcUserManagement2 extends the base interface with SelectUserDB. You must select a database before opening a project. After opening the project, you can switch users, but the database is then locked. LoginUser throws a COMException if the user name is not found or the password is incorrect.
Requirements
- TwinCAT XAE is installed and started.
- A DTE connection is active.
- A user database in the form of a
.tcudbfile exists. SelectUserDBis called before a project is opened if a user-defined database is to be used.
C# sample
The interface is converted to ITcUserManagement2 using a cast. Exception handling is recommended for LoginUser.
// Get ITcUserManagement2 via DTE
ITcUserManagement2 userMgr = (ITcUserManagement2)dte.GetObject("TcUserManagement");
// Select user database (must be done before opening a project)
userMgr.SelectUserDB(@"C:\TwinCAT\3.1\CustomConfig\UserDBs\MyUserDb.tcudb");
// Log in user
userMgr.LoginUser("Administrator", "MyPassword");
// Query active user
string activeUser = userMgr.ActiveUser;
Console.WriteLine("Aktiver Benutzer: " + activeUser);
PowerShell sample
In PowerShell, the interface is obtained via late binding; an explicit cast is not required.
# Get UserManagement interface via DTE
$userMgr = $dte.GetObject("TcUserManagement")
# Check active user (before login)
Write-Host "Aktueller Benutzer: $($userMgr.ActiveUser)"
# Log in
$userMgr.LoginUser("Administrator", "MyPassword")
Write-Host "Eingeloggt als: $($userMgr.ActiveUser)"
![]() |
|
