Skip to content

Commit 0e42413

Browse files
committed
fix: Auth header when downloading images from tfs
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.
1 parent 9f79e9a commit 0e42413

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,15 @@ private string UploadedAndRetrieveAttachmentLinkUrl(string matchedSourceUri, str
132132
{
133133
if (!string.IsNullOrEmpty(sourcePersonalAccessToken))
134134
{
135-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", sourcePersonalAccessToken);
135+
if (!string.IsNullOrEmpty(sourcePersonalAccessToken) && matchedSourceUri.Contains("tfs/DefaultCollection"))
136+
{
137+
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", sourcePersonalAccessToken)));
138+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
139+
}
140+
else if (!string.IsNullOrEmpty(sourcePersonalAccessToken))
141+
{
142+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", sourcePersonalAccessToken);
143+
}
136144
}
137145
var result = DownloadFile(httpClient, matchedSourceUri, fullImageFilePath);
138146
if (!result.IsSuccessStatusCode)

0 commit comments

Comments
 (0)