Skip to content

Commit 0ea6199

Browse files
authored
fix: Auth header when downloading images from tfs (#2877)
Updated the logic for setting the `Authorization` header in `HttpClient` to improve security and flexibility. The code now checks if the `sourcePersonalAccessToken` is not empty and if the `matchedSourceUri` contains "tfs/DefaultCollection". If both conditions are met, the token is encoded in Base64 with an empty username; otherwise, the token is set directly if it's not empty.
2 parents 6688e94 + 2d234a6 commit 0ea6199

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/MigrationTools.Clients.TfsObjectModel/Tools/TfsEmbededImagesTool.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,18 @@ private string UploadedAndRetrieveAttachmentLinkUrl(string matchedSourceUri, str
132132
{
133133
if (!string.IsNullOrEmpty(sourcePersonalAccessToken))
134134
{
135-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", sourcePersonalAccessToken);
135+
var host = new Uri(matchedSourceUri).Host.ToLowerInvariant();
136+
if (host.Contains("dev.azure.com") || host.Contains("visualstudio.com"))
137+
{
138+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", sourcePersonalAccessToken);
139+
}
140+
else
141+
{
142+
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", sourcePersonalAccessToken)));
143+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
144+
}
136145
}
146+
137147
var result = DownloadFile(httpClient, matchedSourceUri, fullImageFilePath);
138148
if (!result.IsSuccessStatusCode)
139149
{

0 commit comments

Comments
 (0)