[英]Is it safe to store confidential settings in .cs file instead Web.config?
[英]Store email credential in web.config file
我正在尝试将 email 凭据从Startup.cs
文件存储到web.config
文件。
var value = ConfigurationManager.AppSettings["appSettings"];
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("service@gmail.com", "123456");
mySmtpClient.Credentials = basicAuthenticationInfo;
mySmtpClient.EnableSsl = true;
mySmtpClient.Port = 587;
MailAddress from = new MailAddress("service@gmail.com", "ActiveDirectoryInformation");
MailAddress to = new MailAddress("test@gmail.com");
//MailAddress to = new MailAddress("it@sarajevoosiguranje.ba");
System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
myMail.Subject = "ActiveDirectory";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
// set body-message and encoding
myMail.Body = @"Ukupno novih korisnika:" + noviKorisnika + "<br>" +
@"Ukupno izmjenjenih korisnika: " + izmjenjenihKorisnika;
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text or html
myMail.IsBodyHtml = true;
我在我的app.config
文件中创建
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="smtpServer" value="smtp.net" />
<add key="Username" value="service@gmail.com"/>
<add key="Password" value="test123456abc"/>
<add key="EnableSsl" value = "true"/>
<add key="smtpPort" value="587" />
<add key="from" value="service@gmail.com" />
<add key="to" value ="test@gmail.com"></add>
</appSettings>
我这样做的原因是我想实时发布这个应用程序,也许将来当我更改sender
或receiver
email 地址时,我不想再次编译应用程序并发布它。
将所有内容存储在 app.config 中并从那里读取是一种简单的方法。
任何建议如何做到这一点?
我不确定这是否适合您,但我能够将 SMTP 信息存储在我的旧 C# 应用程序之一的 app.config 文件中。
但是,To: 地址并未作为凭证的一部分存储,因此我将这些信息存储在本地 SQLite 文件中。
要检索设置,您只需初始化将从本地配置文件读取的SmtpClient
object。
希望这可以帮助。
public static void WriteDefaultSMTPSettingsToConfigurationFile()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MailSettingsSectionGroup mailSettingsSectionGroup = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
SmtpSection smtpSection = mailSettingsSectionGroup.Smtp;
if (!smtpSection.ElementInformation.IsPresent)
{
smtpSection.DeliveryFormat = SmtpDeliveryFormat.International;
smtpSection.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpNetworkElement smtpNetworkElement = smtpSection.Network;
smtpNetworkElement.Host = "localhost";
smtpNetworkElement.Port = 465;
smtpNetworkElement.EnableSsl = true;
smtpNetworkElement.UserName = "username";
smtpNetworkElement.Password = "password";
config.Save();
ConfigurationManager.RefreshSection("system.net/mailSettings");
}
}
据我了解,您希望将信息存储在 App.config 文件中,这很容易。
如果您还没有项目,请将 System.Configuration.dll 引用添加到您的项目。
项目 > myAwsomeProject > 参考 > 添加参考 > 查找 System.Configuration.dll
(或类似的东西)并将其添加到您的项目中。
使用以下命令导入 System.Configuration.dll:
using System.Configuration;
然后创建一个 function 或者在你的 main() 下编写第三步的代码;
创建引用您的 App.config 文件的配置 class 的实例
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
使用以下语法从 App.config 文件中读取
config.AppSettings.Settings["mykey"].Value
您将在其中将“mykey”值替换为相应的键名。 例如,在您的情况下,用户名或密码
使用以下语法写入/更改键的值
config.AppSettings.Settings["mykey"].Value = "value";
“mykey”是键,“value”是所需的值
config.Save();
您还可以在 App.Config 文件中添加和删除密钥
config.AppSettings.Settings.Add("newKey", "value");
config.AppSettings.Settings.Remove("mykey");
再次在有关文件修改的每个操作之后。 保存文件
config.Save();
app.config 文件不是您在 Visual Studio 的解决方案资源管理器中看到的文件。 解决方案资源管理器中的那个即使保存后也不会被修改。
您将使用和修改的 app.config 将位于 C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Debug
或者如果它处于发布模式 C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Release
或者如果您使用的是自定义项目路径
\Path\To\your\Awsome\Project\yourAwsomeProject\bin\Release
该文件将被命名为
yourAwsomeProject.exe.config
(如果我错了或误解了问题,请纠正我)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.