Hi @john john ,
According to your description, I create a simple demo to produce your problem, you could try something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--<sectionGroup name="customAppSettingsGroup" >-->
<section name="customAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<!--</sectionGroup>-->
</configSections>
<!--<customAppSettingsGroup>-->
<customAppSettings>
<add key="APIKey" value="*****" />
</customAppSettings>
<!--</customAppSettingsGroup>-->
<appSettings>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
And use these code to get the key value:
NameValueCollection section = ConfigurationManager.GetSection("customAppSettings") as NameValueCollection;
string key = section["APIKey"];
Regarding its encryption and decryption, I think you could try to use SectionInformation.ProtectSection(String) Method. About how to call Data Protection API (DPAPI), you could read this bolg to learn a complete example: How to encrypt and decrypt data using DPAPI in C# or VB.NET
Best regards,
Xudong Peng
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.