Use the TryLogin
method to login to the client. The TryLogin
method has two overloads. The first signature uses Microsoft Credential ManagerĀ® to read the credentials, and the second signature takes a username and password.
After you log in, you receive an access token that is part of the authorization process that expires after 30 minutes. The login method takes your username, password, and an optional parameter that it later uses in the refresh token API call. To request a new access token prior to expiration, use the refresh token API method.
For more information on how to use the Credential Manager, see https://support.microsoft.com/en-us/help/4026814/windows-accessing-credential-manager
Log in credentials
- Application with user interaction.
- Application without user interaction.
Application with user interaction: create a prompt for a user to enter their credentials, which they use to log in.
// Automatically refresh token using plain credentials
client.TryLogin("username", "password");
// Do not automatically refresh token using plain credentials
client.TryLogin("username", "password", false);
Application without user interaction: place the log in credentials in a Credential Manager target. The library supports retrieving credentials from a predefined target. For example, for an energy monitoring application, create an entry named metasys-energy-app
and configure privileges for this application independently of any human users, or other applications that you write. When you create an independent user, it improves the usefulness of the audit trail entries, within the Metasys system.
For an example of a custom application without user interaction that uses the entry metasys-energy-app
, see the following .NET C# code:
// Read target from Credential Manager and automatically refresh token
client.TryLogin("metasys-energy-app");
// Read target from Credential Manager and do not refresh token
client.TryLogin("metasys-energy-app", false);
AccessToken accessToken = client.TryLogin("metasys-energy-app");
Console.WriteLine(accessToken);
/*
{
"Issuer": "metasysserver",
"IssuedTo": "metasysapiuser",
"Token": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkJzR0lWelVZcjN0MkI0RGRtT1ljMTdBLVZJOCIsImtpZC...",
"Expires": "2020-05-12T14:18:51Z"
}
*/
client.Refresh();