Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have added a custom section called secureAppSettings to my web.config file:

<configuration>
  <configSections>
    <section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <secureAppSettings>
    <add key="userName" value="username"/>
    <add key="userPassword" value="password"/>
  </secureAppSettings>  
</configuration>

secureAppSettings is decrypted and has two keys inside it.

Now in my code, I tried to access the keys like this:

string userName = System.Configuration.ConfigurationManager.secureAppSettings["userName"];
string userPassword = System.Configuration.ConfigurationManager.secureAppSettings["userPassword"];

But null is returning for these fields.

How can I get the field values?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

You could access them as key/value pairs:

NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("secureAppSettings");
string userName = section["userName"];
string userPassword = section["userPassword"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...