Differences
This shows you the differences between the selected revisions of the page.
| 2015-11-03 | 2015-11-04 | ||
| securepassword (martin) | c#/vb.net (martin) | ||
| Line 83: | Line 83: | ||
| <code powershell> | <code powershell> | ||
| $sessionOptions.SecurePassword = ConvertTo-SecureString $config.Configuration.Password | $sessionOptions.SecurePassword = ConvertTo-SecureString $config.Configuration.Password | ||
| + | </code> | ||
| + | |||
| + | ==== Visual Studio (C#, VB.NET) ==== | ||
| + | |||
| + | In .NET projects (C#, VB.NET) an application configuration file (''App.config'') is used to store the settings. Though this file contains other configuration that needs to be shared and/or stored in a revision control system. To separate the credentials from other settings, you can link another configuration file like shown below. | ||
| + | |||
| + | Use the ''file'' attribute of ''appSettings'' element in the primary application configuration file (''App.config''): | ||
| + | |||
| + | <code xml> | ||
| + | <?xml version="1.0" encoding="utf-8" ?> | ||
| + | <configuration> | ||
| + | <startup> | ||
| + | <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> | ||
| + | </startup> | ||
| + | <appSettings file="ProtectedConfig.config"> | ||
| + | <!-- other settings --> | ||
| + | </appSettings> | ||
| + | </configuration> | ||
| + | </code> | ||
| + | |||
| + | Add a new %%XML%% file to the project and name it ''ProtectedConfig.config'': | ||
| + | |||
| + | <code xml> | ||
| + | <?xml version="1.0" encoding="utf-8" ?> | ||
| + | <appSettings> | ||
| + | <add key="UserName" value="martin" /> | ||
| + | <add key="Password" value="test" /> | ||
| + | </appSettings> | ||
| + | </code> | ||
| + | |||
| + | Set the //Copy to Output Directory// property of the file to //Copy if newer// and the //Build Action// to //Content// to have the new configuration file correctly deployed. | ||
| + | |||
| + | To read the settings from the configuration file use ''[[https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx|ConfigurationManager.AppSettings]]'':((You need to reference the ''System.configuration'' assembly in your project to use the ''ConfigurationManager'' class.)) | ||
| + | |||
| + | <code csharp> | ||
| + | SessionOptions sessionOptions = new SessionOptions | ||
| + | { | ||
| + | Protocol = Protocol.Sftp, | ||
| + | HostName = "example.com", | ||
| + | UserName = ConfigurationManager.AppSettings["UserName"], | ||
| + | Password = ConfigurationManager.AppSettings["Password"], | ||
| + | }; | ||
| + | </code> | ||
| + | |||
| + | You can also leverage Windows Data Protection API to encrypt the password in the XML file. | ||
| + | |||
| + | To encrypt the password use PowerShell ''ConvertFrom-SecureString'' cmdlet: | ||
| + | |||
| + | <code> | ||
| + | powershell.exe -Command "& Read-Host -AsSecureString | ConvertFrom-SecureString" | ||
| + | </code> | ||
| + | |||
| + | A password encrypted this way can be decrypted by the same Windows account only. | ||
| + | |||
| + | Store the encrypted password to the %%XML%% file instead of the plain-text one: | ||
| + | |||
| + | <code xml> | ||
| + | <?xml version="1.0" encoding="utf-8" ?> | ||
| + | <appSettings> | ||
| + | <add key="UserName" value="martin" /> | ||
| + | <add key="Password" value="01000000d08c9ddf0115d1118c7a00c04fc297eb01000000cf6dbc52515..." /> | ||
| + | </appSettings> | ||
| + | </code> | ||
| + | |||
| + | To decrypt the password, use ''[[https://msdn.microsoft.com/en-us/library/xh68ketz.aspx|ProtectedData.Unprotect]]'':((You need to reference the ''System.Security'' assembly in your project to use the ''ProtectedData'' class.)) | ||
| + | |||
| + | <code csharp> | ||
| + | string hex = ConfigurationManager.AppSettings["Password"]; | ||
| + | byte[] bytes = Enumerable.Range(0, hex.Length / 2).Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16)).ToArray(); | ||
| + | byte[] decrypted = ProtectedData.Unprotect(bytes, null, DataProtectionScope.CurrentUser); | ||
| + | sessionOptions.Password = Encoding.Unicode.GetString(decrypted); | ||
| </code> | </code> | ||