Protecting an email password when using NLog

纵饮孤独 提交于 2019-12-06 10:26:29

问题


When using NLog as a logging instrument we can easily send messages via email, for example this is the example configuration for using Gmail as a smtp server:

<targets>
<target name="gmail" type="Mail"
        smtpServer="smtp.gmail.com"
        smtpPort="587"
        smtpAuthentication="Basic"
        smtpUsername="user@gmail.com"
        smtpPassword="password"
        enableSsl="true"
        from="emailaddress@gmail.com"
        to="recipient@example.com"
        cc="alice@example.com;bob@example.com;charlie@example.com"
      />
</targets>
<rules>
 <logger name="*" minlevel="Debug" writeTo="gmail" />
</rules>

It works like a charm. But in the above example the password is put in plain text in a configuration file.
Is there a way to protect it somehow?


回答1:


Yes, you can move the NLog.config (If you have it in this file) to your app.config and then encrypt you app.config.

You can see how to encrypt the app.config here.




回答2:


You could configure the logger inside code. There, you can even hide your password from anyone with a hex-editor! Plus, nobody gets any chance to mess with your config file.

Public Class MyEmailLoggerClass
    Public Sub New()
        SetNlogConfiguration()
    End Sub

    Private Sub SetNlogConfiguration()
        Private MyNLogConfiguration As New LoggingConfiguration()
        Dim myemailtarget As New MailTarget()
        Dim MailRule As New LoggingRule("myemail", LogLevel.Info, myemailtarget) 

        With myemailtarget
            .SmtpServer = "mail.724hosting.com"
            .SmtpAuthentication = SmtpAuthenticationMode.Basic
            .SmtpUserName = "myemail" & "@" & "somewhere.com"
            .SmtpPassword = "my" & "pass" & "word"
            .SmtpPort = 587
            '.Encoding = System.Text.Encoding.UTF8
            .EnableSsl = true
            .AddNewLines = True
            .Html = True
            .Layout = "${message}"
            'message options
            .To = "sometech" & "@" & "somewhere.com"
            .cc = "bob@somewhere.com,harry@somewhereelse.com"
            .From = "myapplication@here.com"
            .Header = "<h2 style='color:red'>Message from myapplication!</h2>"
            .Subject = "Report from " & myapplicationname & " on someone's computer" & ${date:F}"
            .Body = "${message}"
        End With

        LogManager.Configuration = myNlogConfig
    End Sub
End Class

To use it, put this in the sub where you want to send email:

    Public MainLogger As New MyEmailLoggerClass
    Dim Logger = LogManager.GetLogger("myemail")
    Logger.Info("Hi, this is a message from my application")


来源:https://stackoverflow.com/questions/6621350/protecting-an-email-password-when-using-nlog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!