I have a library for sending email within software (a wrapper to MailKit). The parameters for the email process (server, user name, password, etc) are defined within appsetting.json:
"DpEmail": {
"SMTP": {
"Host_Address": "mail.myServer.net",
"Host_Port": "465",
"Host_UserName": "a@b.c",
"Host_Password": "thePassword",
"Sender_Email": "noreply@myDomain.eu",
"Sender_Name": "NoReply"
}
},
I define a class to contain the options:
public class MailSenderOptions { //mirrors elements of appsettings.json section }
And set up the mail service using the options pattern:
services.Configure<MailSenderOptions> (GetSection ("DpEmail:SMTP"));
services.AddSingleton<IDpEmail, DpMailLib.DpEmail> ();
This all works fine for development but, in production, for security I would want to provide passwords from other sources (eg app arguments, environment variables). The same would apply to database passwords, etc.
My question is how, using best practice, I could intervene in the Options Pattern, to set the password in the MailSenderOptions instance within DI that I have obtained securely from a source other than the appsettings file, while the non-confidential items come from that file. Is there a sensible alternative to injecting the password separately?