Symptom
Normally, you can configure the application.json to change the password, and then restart the Web API. Sometimes, you may need to change the database password dynamically when PowerServer Web API is running.
E.g.: the password is not allowed to be stored in the Configuration file due to security policies, the database password needs to be obtained from a third party.
Environment
PowerServer 2022 R3
Resolution
Solution 1. Storing database connections in the database.
Refer to Storing database connections in the database - - PowerServer 2022 R3 Help
Solution 2. Write your own logic to get the password.
This is an example based on the Sales App.
Step 1. Follow PS 2022 R3 Quick Start to make sure the Example Sales App can run successfully in PowerBuilder IDE.
https://docs.appeon.com/ps2022r3/quick_start_guide1.html
Step 2. Change the C# solution
a. After the application is deployed, open the C# solution and add the class AppUserConfigurationProvider.cs to the C# solution >> UserExtensions\AppConfig folder. You can add your own logic in the GetDbPassword() method to change the password for a database connection.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using PowerServer.Core; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration.Json; namespace ServerAPIs { public class AppUserConfigurationProvider : FileSystemConfigurationProvider { private readonly IHttpContextAccessor _httpContextAccessor; public AppUserConfigurationProvider(IConfiguration configuration) : base(Path.Combine(AppContext.BaseDirectory, "appconfig\\applications.json"), new ArgumentsConfigurationProvider(configuration)) { } public async override Task<IEnumerable<ConnectionConfigurationItem>>GetConnectionsAsync( string cacheGroup, System.Threading.CancellationToken cancellationToken) { // Get a sequence of connection configurations that belong to a cache group. var dbConnectons = await base.GetConnectionsAsync(cacheGroup, cancellationToken); return dbConnectons.Select(o => { // Refresh the password of each database cache name. o.Configuration.Password = GetDbPassword(o.CacheName); return o; }); } private string GetDbPassword(string cacheName) { // Add your own logic here to get the new password. // ... // E.g.: if (cacheName == "sales") { return "sql"; } else { return ""; } } } }
b. Rewrite the UserExtensions\AppConfig\ AppConfigExtensions.cs file in the C# solution.
Notice that all Code blocks generated by default have already been removed.
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using PowerServer.Core; namespace ServerAPIs { public static class AppConfigExtensions { //To be called in Startup.ConfigureServices, for adding services related to the PowerServer configuration management module public static IServiceCollection AddPowerServerAppConfig( this IServiceCollection services, IConfiguration configuration, IHostEnvironment hostingEnvironment) { // Use a customized AppConfig provider. services.AddSingleton<IPowerServerConfigurationProvider, AppUserConfigurationProvider>(); return services; } } }