Matt
Matt

Reputation: 1704

Custom app.config section fails to cast to NameValueCollection

This tutorial makes what I'm trying to do look dead easy. All I want to do is read a custom attribute out of my web.config. Here's the relevant part:

<configSections>
    <section name="Authentication.WSFedShell" type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>

<Authentication.WSFedShell>
    <add key="Authentication.PrincipalType" value="ClientCertificate" />
</Authentication.WSFedShell>

In the immediate window I can execute:

System.Configuration.ConfigurationManager.GetSection("Authentication.WSFedShell")

which returns the string

["Authentication.PrincipalType"]: "ClientCertificate"

However, when I try to cast it (with as NameValueCollection), as this tutorial says to do, I get null returned and my code blows up. There's gotta be a cleaner way to get the value "ClientCertificate" than manually parsing the string result.

How do I read "ClientCertificate" from app.config?

Upvotes: 0

Views: 822

Answers (1)

Rahul
Rahul

Reputation: 77876

Why can't you use AppSetting like

<configuration>
  <appSettings>
    <add key="Authentication.PrincipalType" value="ClientCertificate"/>
  </appSettings>
</configuration>

   System.Configuration.ConfigurationManager.AppSettings["Authentication.PrincipalType"]

Most probably the issue with your section is the Type attribute. But anyways, you need to cast the result of GetSection() to your type defined for section like

System.Configuration.DictionarySectionHandler config = (System.Configuration.DictionarySectionHandler)System.Configuration.ConfigurationManager.GetSection("Authentication.WSFedShell");

Upvotes: 2

Related Questions