|
17 | 17 | using System;
|
18 | 18 | using System.Linq;
|
19 | 19 | using System.Net;
|
| 20 | +using System.Text; |
20 | 21 | using System.Text.RegularExpressions;
|
21 | 22 | using chocolatey.infrastructure.commandline;
|
22 | 23 | using chocolatey.infrastructure.app.configuration;
|
@@ -81,7 +82,7 @@ public Task<CredentialResponse> GetAsync(Uri uri, IWebProxy proxy, CredentialReq
|
81 | 82 | {
|
82 | 83 | this.Log().Debug("Using passed in credentials");
|
83 | 84 |
|
84 |
| - return Task.FromResult(new CredentialResponse(new NetworkCredential(_config.SourceCommand.Username, _config.SourceCommand.Password))); |
| 85 | + return Task.FromResult(new CredentialResponse(ToNetworkCredentials(_config.SourceCommand.Username, _config.SourceCommand.Password, isRetry))); |
85 | 86 | }
|
86 | 87 | }
|
87 | 88 |
|
@@ -156,7 +157,9 @@ public Task<CredentialResponse> GetAsync(Uri uri, IWebProxy proxy, CredentialReq
|
156 | 157 | this.Log().Debug("Using saved credentials");
|
157 | 158 | }
|
158 | 159 |
|
159 |
| - return Task.FromResult(new CredentialResponse(new NetworkCredential(source.Username, NugetEncryptionUtility.DecryptString(source.EncryptedPassword)))); |
| 160 | + var credentials = ToNetworkCredentials(source.Username, NugetEncryptionUtility.DecryptString(source.EncryptedPassword), isRetry); |
| 161 | + |
| 162 | + return Task.FromResult(new CredentialResponse(credentials)); |
160 | 163 | }
|
161 | 164 |
|
162 | 165 | #pragma warning disable IDE0060 // unused method parameter
|
@@ -186,15 +189,54 @@ public ICredentials GetUserCredentials(Uri uri, IWebProxy proxy, CredentialReque
|
186 | 189 | return CredentialCache.DefaultNetworkCredentials;
|
187 | 190 | }
|
188 | 191 |
|
189 |
| - var credentials = new NetworkCredential |
190 |
| - { |
191 |
| - UserName = username, |
192 |
| - Password = password |
193 |
| - }; |
| 192 | + var credentials = ToNetworkCredentials(username, password, isRetry: false); |
194 | 193 |
|
195 | 194 | return credentials;
|
196 | 195 | }
|
197 | 196 |
|
| 197 | + private static NetworkCredential ToNetworkCredentials(string username, string password, bool isRetry) |
| 198 | + { |
| 199 | + if (isRetry) |
| 200 | + { |
| 201 | + // If we have already attempted using the fallback legacy |
| 202 | + // way to set the string, then we will just return the network |
| 203 | + // credentials as is, and assume that the server expects the |
| 204 | + // encoding used to be the same as the bug in .NET 4.8.1 that |
| 205 | + // causes incorrect encodings to be used. |
| 206 | + |
| 207 | + return new NetworkCredential(username, password); |
| 208 | + } |
| 209 | + |
| 210 | + var utf8Bytes = Encoding.UTF8.GetBytes(password); |
| 211 | + try |
| 212 | + { |
| 213 | + var legacyEncoding = GetEncoding(1252); |
| 214 | + |
| 215 | + var legacyPassword = legacyEncoding.GetString(utf8Bytes); |
| 216 | + |
| 217 | + return new NetworkCredential(username, legacyPassword); |
| 218 | + } |
| 219 | + catch |
| 220 | + { |
| 221 | + return new NetworkCredential(username, password); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private static Encoding GetEncoding(int codepage) |
| 226 | + { |
| 227 | + try |
| 228 | + { |
| 229 | + return Encoding.GetEncoding(codepage); |
| 230 | + } |
| 231 | + catch |
| 232 | + { |
| 233 | + // If the code page specified isn't available, |
| 234 | + // then let us fall back to the default encoding |
| 235 | + // used on the system. |
| 236 | + return Encoding.Default; |
| 237 | + } |
| 238 | + } |
| 239 | + |
198 | 240 | #pragma warning disable IDE0022, IDE1006
|
199 | 241 | [Obsolete("This overload is deprecated and will be removed in v3.")]
|
200 | 242 | public ICredentials get_credentials_from_user(Uri uri, IWebProxy proxy, CredentialRequestType credentialType)
|
|
0 commit comments