Lectura system.net/mailSettings/smtp de Web.configuración en un entorno de confianza medio
Tengo un código heredado que almacena servidor SMTP, nombre de usuario, contraseña en la sección system.net/mailSettings/smtp
de la Web.config.
Solía leerlos así:
Configuration c = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)c.GetSectionGroup("system.net/mailSettings");
return settings.Smtp.Network.Host;
Pero esto estaba fallando cuando tuve que implementar en un entorno de confianza media.
Así que siguiendo la respuesta de esta pregunta , la reescribí para usar GetSection()
así:
SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
return settings.Network.Host;
Pero todavía me está dando una SecurityException en Medium trust, con el siguiente mensaje:
Solicitud de Permisos de configuración error al intentar acceder sección de configuración 'system.net/mailSettings/smtp". To permitir que todas las personas que llaman accedan a los datos para esta sección, establecer sección atributo 'requirePermission' igual 'false' en el archivo de configuración donde se declara esta sección.
Así que probé este atributo requirePermission
, pero no puedo averiguar dónde ponerlo.
Si lo aplico al nodo
Si lo aplico al nodo
¿Hay alguna manera de llegar a esta sección de configuración programáticamente bajo medium trust? ¿O simplemente debo renunciar a él y mover la configuración a
5 answers
El atributo requirePemission va en la agrupación <configSections>
que coincide con la parte de la web.config que está teniendo el problema de seguridad con.
Además, no tiene que leer la configuración usando código para enviar correo-simplemente puede usar un SmtpClient en blanco:
new SmtpClient.Send(MyMailMessage);
Se enviará usando la configuración de las secciones de configuración por defecto.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-05 03:11:39
Puede crear un SmtpClient como algunos sugirieron, pero eso es un poco exagerado - solo lea las secciones directamente.
var section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
var host=section.Network.Host
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-13 18:33:30
Esto funciona muy bien para mí.
var smtp = new System.Net.Mail.SmtpClient();
var host = smtp.Host;
var ssl = smtp.EnableSsl;
var port = smtp.Port;
var credential = new System.Net.Configuration.SmtpSection().Network;
var username = credential.UserName;
var password = credential.Password;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-18 10:17:16
Alegrías de la codificación eh... siempre 1000 maneras de pelar un pez
System.Net.Configuration.SmtpSection smtp = new System.Net.Configuration.SmtpSection();
string from = smtp.From;
//etc
System.Net.Configuration.SmtpNetworkElement nt = new System.Net.Configuration.SmtpNetworkElement();
string host = nt.Host;
//etc
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-09 10:02:40
Para obtener la configuración de las secciones de correo, simplemente cree los objetos de correo.
var client = new SmtpClient();
var messageSettings = new MailMessage();
var host=client.Host;
//etc...
var fromAddress=messageSettings.From.Address;
//etc..
Config:
<system.net>
<mailSettings>
<smtp from="[email protected]" deliveryMethod="Network" >
<network host="smtp.mail.yahoo.com" port="587" enableSsl="true"
userName="[email protected]" password="xxxxxxx"/>
</smtp>
</mailSettings>
</system.net>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-17 08:21:48