Login method - Metasys - LIT-12013522 - Software Application - Basic Services client library for .NET (and COM) - Metasys API - 4.2

Metasys REST Client for .NET and COM Developer Guide

Brand
Metasys
Product name
Basic Services client library for .NET (and COM)
Metasys API
Document type
API Reference Guide
Document number
LIT-12013522
Version
4.2
Revision date
2021-04-07
Language
English

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

Depending on the type of application you are writing, choose from one of the following options:
  • 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.

Use the following .NET C# code to log in if the username and password reside in 'username' and 'password' variables.
// 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.

Important: Do not hard code credentials into an application.

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"
  }
*/
To manually refresh the access token before it expires, use the following .NET C# code:
client.Refresh();