在中等信任环境中从Web.config读取system.net/mailSettings/smtp [英] Reading system.net/mailSettings/smtp from Web.config in Medium trust environment
问题描述
我有一些继承的代码,它们在Web.config的system.net/mailSettings/smtp
部分中存储SMTP服务器,用户名和密码.
I have some inherited code which stores SMTP server, username, password in the system.net/mailSettings/smtp
section of the Web.config.
它曾经这样读取它们:
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.
因此,按照此问题的回答 ,我将其重写为像这样使用GetSection()
:
SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
return settings.Network.Host;
但是在中等信任的情况下,它仍然给我一个SecurityException,并显示以下消息:
But it's still giving me a SecurityException on Medium trust, with the following message:
请求ConfigurationPermission 尝试访问时失败 配置部分 'system.net/mailSettings/smtp'.到 允许所有呼叫者访问数据 对于此部分,请设置部分 属性'requirePermission'等于 配置文件中的"false" 声明此部分的地方.
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.
因此,我尝试了此requirePermission
属性,但无法弄清楚将其放置在何处.
So I tried this requirePermission
attribute, but can't figure out where to put it.
如果我将其应用于< smtp>节点,我收到一个ConfigurationError:无法识别的属性'requirePermission'.请注意,属性名称区分大小写."
If I apply it to the <smtp> node, I get a ConfigurationError: "Unrecognized attribute 'requirePermission'. Note that attribute names are case-sensitive."
如果我将其应用于< mailSettings>节点,我仍然收到SecurityException.
If I apply it to the <mailSettings> node, I still get the SecurityException.
在中等信任度下,有什么方法可以通过编程方式到达此配置节吗?还是我应该放弃它,然后将设置移至< appSettings>?
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>?
推荐答案
requirePemission属性出现在<configSections>
分组中,该分组与您遇到安全问题的web.config部分匹配.
The requirePemission attribute goes on the <configSections>
grouping that matches the part fo the web.config you are having the security issue with.
此外,您不必使用代码来实际阅读设置即可发送邮件-您只需使用空白的SmtpClient:
additionally, you don't have to actually read the settings using code to send mail - you can simply use a blank SmtpClient:
new SmtpClient.Send(MyMailMessage);
默认情况下,它将使用config部分中的设置进行发送.
it will send using the settings from the config sections by default.
这篇关于在中等信任环境中从Web.config读取system.net/mailSettings/smtp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!