I have some inherited code which stores SMTP server, username, password in the system.net/mailSettings/smtp
section of the Web.config.
It used to read them like so:
Configuration c = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)c.GetSectionGroup("system.net/mailSettings");
return settings.Smtp.Network.Host;
But this was failing when I had to deploy to a medium trust environment.
So following the answer from this question, I rewrote it to use GetSection()
like so:
SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
return settings.Network.Host;
But it's still giving me a SecurityException on Medium trust, with the following message:
Request for ConfigurationPermission failed while attempting to access configuration section 'system.net/mailSettings/smtp'. To allow all callers to access the data for this section, set section attribute 'requirePermission' equal 'false' in the configuration file where this section is declared.
So I tried this requirePermission
attribute, but can't figure out where to put it.
If I apply it to the <smtp> node, I get a ConfigurationError: "Unrecognized attribute 'requirePermission'. Note that attribute names are case-sensitive."
If I apply it to the <mailSettings> node, I still get the SecurityException.
Is there any way to get at this config section programatically under medium trust? Or should I just give up on it and move the setting into <appSettings>?