Web.configのsystem.net/mailSettings/smtp
セクションにSMTPサーバー、ユーザー名、パスワードを格納する継承コードがあります。中程度の信頼環境のWeb.configからsystem.net/mailSettings/smtpを読み込みます。
それはそうのようにそれらを読み取るために使用:
Configuration c = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)c.GetSectionGroup("system.net/mailSettings");
return settings.Smtp.Network.Host;
しかし、私はメディアの信頼環境にデプロイするために持っていたとき、これは失敗でした。
のでthis questionからの回答、以下、私はそうのようなGetSection()
を使用するように書き直し:
SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
return settings.Network.Host;
しかし、それはまだ、次のメッセージを表示して、私の中程度の信頼にSecurityExceptionを与えている:
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
属性を試しましたが、どこに置くべきか分かりません。
ノードに<のsmtp >ノードを適用すると、ConfigurationErrorが表示されます。「認識できない属性 'requirePermission'」属性名では大文字と小文字が区別されます。
<mailSettings>ノードに適用すると、引き続きSecurityExceptionが発生します。
このコンフィグレーションセクションでプログラム的に中信頼状態になる方法はありますか?または、私はそれをあきらめて、設定を<appSettings>に移動する必要がありますか?
たびI Iを設定すると、 "それを読む" する必要はありませんでしたということでした。新しいSmtpClient()を作成するだけで、web.configの設定が使用されます。 – turtlepick