private void TryLogin_Refresh() { /* SNIPPET 1: START */ // Automatically refresh token using plain credentials client.TryLogin("username", "password"); // Do not automatically refresh token using plain credentials client.TryLogin("username", "password", false); /* SNIPPET 1: END */ /* SNIPPET 2: START */ // 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); /* SNIPPET 2: END */ /* SNIPPET 3: START */ AccessToken accessToken = client.TryLogin("metasys-energy-app"); Console.WriteLine(accessToken); /* * { * "Issuer": "metasysserver", * "IssuedTo": "metasysapiuser", * "Token": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkJzR0lWelVZcjN0MkI0RGRtT1ljMTdBLVZJOCIsImtpZCI6IkJzR0lWelVZcjN0MkI0RGRtT1ljMTdBLVZJOCJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0Ojk1MDYvQVBJLkF1dGhlbnRpY2F0aW9uU2VydmljZSIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6OTUwNi9BUEkuQXV0aGVudGljYXRpb25TZXJ2aWNlL3Jlc291cmNlcyIsImV4cCI6MTU4OTI5MzEzMCwibmJmIjoxNTg5MjkxMzMwLCJjbGllbnRfaWQiOiJtZXRhc3lzX3VpIiwic2NvcGUiOlsibWV0YXN5c19hcGkiLCJvZmZsaW5lX2FjY2VzcyIsIm9wZW5pZCJdLCJzdWIiOiI4ZGE4YjE4Yy1lMTk1LTRlMmMtOGU2Zi0zNTE2Zjc0ZWFhNGIiLCJhdXRoX3RpbWUiOjE1ODkyOTEzMzAsImlkcCI6Imlkc3J2IiwiVXNlcklkIjoiMSIsIlVzZXJOYW1lIjoibWV0YXN5c3N5c2FnZW50IiwiSXNBZG1pbiI6IlRydWUiLCJJc1Bhc3N3b3JkQ2hhbmdlUmVxdWlyZWQiOiJGYWxzZSIsIklzVGVybXNBbmRDb25kaXRpb25zUmVxdWlyZWQiOiJGYWxzZSIsIkxpY2Vuc2VJbmZvIjoie1wiSXNMaWNlbnNlZFwiOnRydWUsXCJMaWNlbnNlVHlwZVwiOlwiZ3JhY2VcIn0iLCJDdWx0dXJlIjoiZW4tVVMiLCJhbXIiOlsicGFzc3dvcmQiXX0.egzw1bs1831pEBWWXbBOYWGU5wFsI3sEnL7RgCIHHbHmcxtpPPqLq54znpoUoLFrMUeymZj5rkrt_mF-CNIpCE3halZNAH-CR1U46LTZi5CMaDfYlP-wHxikAGV5GwFjlHjGNOUaFtd7n4yC5sH08pHQfXXD5gKDm_FVMfUJXAo-E8gmrkU0wMn5U2FRyQyj7Yhq6jaj7MPTF__Xz46sG3WtDr45WK2NmuwiLDv408URZ5fJxlMngRpjSIONHVAIwna_H0AguHiIELkvuRVYcRqIH5kb1YdFt-3fsnTV9xwpozZZ44dh-4I7x466I-UGlLHAnScWILUbPcpRNWm0Uw", * "Expires": "2020-05-12T14:18:51Z" * } */ /* SNIPPET 3: END */ /* SNIPPET 4: START */ client.Refresh(); /* SNIPPET 4: END */ }
/// <summary> /// Use to setup client when the AccessToken is being tested. /// </summary> protected void CleanLogin() { httpTest.RespondWithJson(new { accessToken = "cleanfaketoken", expires = date1 }); client.TryLogin("cleanusername", "cleanpassword"); }
static void Main(string[] args) { var log = new LogInitializer(typeof(Program)); string connectionDetails; try { if (args.Length != 3) { Console.WriteLine("Please enter in your credentials in this format: {username} {password} {hostname} {api_version} or as an alternative you can specify just the Credential Manager target and the hostname in this way {credmantarget} {hostname} {api_version}." + "\nRefer to the metasys-server/basic-services-dotnet README if you need help getting started."); connectionDetails = Console.ReadLine(); args = connectionDetails.Split(' '); } string username = null, password = null, hostname = null, credManTarget = null; string version = null; if (args.Length > 3) { username = args[0]; password = args[1]; hostname = args[2]; version = args[3]; } else { credManTarget = args[0]; hostname = args[1]; version = args[2]; } #region Login Console.WriteLine("Default culture is en_US. The culture for client translations can be changed in the code."); // CultureInfo culture = new CultureInfo("en-US"); Console.WriteLine("\nLogging in..."); var apiVersion = (ApiVersion)Enum.Parse(typeof(ApiVersion), version); var client = new MetasysClient(hostname, true, apiVersion, logClientErrors: false); // Disable default logging since it is handled in this app. // var client = new MetasysClient(hostname, true); // Ignore Certificate Errors // var client = new MetasysClient(hostname, false, ApiVersion.v2, culture); AccessToken token; if (string.IsNullOrWhiteSpace(credManTarget)) { token = client.TryLogin(username, password); } else { // Read and login using cred managerfrom Credential manager token = client.TryLogin(credManTarget); } Console.WriteLine($"Access token: {token.Token} expires {token.Expires}."); #endregion bool showMenu = true; while (showMenu) { showMenu = MainMenu(client); } } catch (Exception exception) { log.Logger.Error(string.Format("An error occured while login - {0}", exception.Message)); Console.WriteLine("\n \nAn Error occurred. Press Enter to exit"); Console.ReadLine(); } }
static void Main(string[] args) { // Add support for JSON Config File IConfiguration config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("AppSettings.json", optional: false, reloadOnChange: true) .Build(); // Read hostname from credential manager first var hostnameTarget = config.GetSection("CredentialManager:Targets:MetasysServer").Value; // Hostname is stored in the Credential Manager using password field var secureHostname = CredentialUtil.GetCredential(hostnameTarget); MetasysClient metasysClient = new MetasysClient(CredentialUtil.convertToUnSecureString(secureHostname.Password)); // Retrieve Metasys Credentials to login var credTarget = config.GetSection("CredentialManager:Targets:MetasysCredentials").Value; // Use TryLogin overload that accepts Credential Manager Target metasysClient.TryLogin(credTarget); // Forecast container target is securely stored in Credential manager var containerTarget = config.GetSection("CredentialManager:Targets:ForecastContainer").Value; var secureContainer = CredentialUtil.GetCredential(containerTarget); // Get parent object of Weather Forecast to retrieve related children (securely stored in Credential Manager) Guid parentObjectId = metasysClient.GetObjectIdentifier(CredentialUtil.convertToUnSecureString(secureContainer.Password)); IEnumerable <MetasysObject> weatherForecast = metasysClient.GetObjects(parentObjectId, 1); // Retrieve latitude and longitude to get weather forecast MetasysObject latitudePoint = weatherForecast.FindByName("Latitude"); MetasysObject longitudePoint = weatherForecast.FindByName("Longitude"); double latitude = metasysClient.ReadProperty(latitudePoint.Id, "presentValue").NumericValue; double longitude = metasysClient.ReadProperty(longitudePoint.Id, "presentValue").NumericValue; // Forecast API key is securely stored in Credential manager var apiKeyTarget = config.GetSection("CredentialManager:Targets:OpenWeather").Value; var secureApiKey = CredentialUtil.GetCredential(apiKeyTarget); // Get Next Five Days forecast every 3 hours OpenWeatherMapClient weatherMapclient = new OpenWeatherMapClient(CredentialUtil.convertToUnSecureString(secureApiKey.Password)); ForecastResult forecastResult = weatherMapclient.GetForecast(latitude, longitude).GetAwaiter().GetResult(); // Get the closest forecast to be written Forecast forecast = forecastResult.list.First(); // Read Metasys points to write the response back MetasysObject Day = weatherForecast.FindByName("Day"); MetasysObject Month = weatherForecast.FindByName("Month"); MetasysObject Year = weatherForecast.FindByName("Year"); MetasysObject Hour = weatherForecast.FindByName("Hour"); MetasysObject Minute = weatherForecast.FindByName("Minute"); MetasysObject Temperature = weatherForecast.FindByName("Temperature"); MetasysObject Humidity = weatherForecast.FindByName("Humidity"); MetasysObject Rain = weatherForecast.FindByName("Rain"); MetasysObject Snow = weatherForecast.FindByName("Snow"); // Use commands to write the results string adjustCommand = "Adjust"; var date = OpenWeatherMapClient.UnixTimeStampToDateTime(forecast.dt); metasysClient.SendCommand(Day.Id, adjustCommand, new List <object> { date.Day }); metasysClient.SendCommand(Month.Id, adjustCommand, new List <object> { date.Month }); metasysClient.SendCommand(Year.Id, adjustCommand, new List <object> { date.Year }); metasysClient.SendCommand(Hour.Id, adjustCommand, new List <object> { date.Hour }); metasysClient.SendCommand(Minute.Id, adjustCommand, new List <object> { date.Minute }); metasysClient.SendCommand(Temperature.Id, adjustCommand, new List <object> { forecast.main.temp }); metasysClient.SendCommand(Humidity.Id, adjustCommand, new List <object> { forecast.main.humidity }); if (forecast.rain != null) { metasysClient.SendCommand(Rain.Id, adjustCommand, new List <object> { forecast.rain.ThreeHours }); } else { // Reset values in case there is no rain metasysClient.SendCommand(Rain.Id, adjustCommand, new List <object> { 0 }); } if (forecast.snow != null) { metasysClient.SendCommand(Snow.Id, adjustCommand, new List <object> { forecast.snow.ThreeHours }); } else { // Reset values in case there is no snow metasysClient.SendCommand(Rain.Id, adjustCommand, new List <object> { 0 }); } }