44using System . Net ;
55using System . Net . Http ;
66using System . Net . Http . Headers ;
7+ using System . Text ;
78using System . Text . RegularExpressions ;
89using Microsoft . Extensions . Logging ;
910using Microsoft . Extensions . Options ;
1213using Microsoft . TeamFoundation . WorkItemTracking . WebApi ;
1314using MigrationTools . DataContracts ;
1415using MigrationTools . Endpoints ;
16+ using MigrationTools . Options ;
1517using MigrationTools . Processors . Infrastructure ;
1618using MigrationTools . Tools . Infrastructure ;
1719
@@ -24,7 +26,6 @@ public class TfsEmbededImagesTool : EmbededImagesRepairToolBase<TfsEmbededImages
2426 private const string TargetDummyWorkItemTitle = "***** DELETE THIS - Migration Tool Generated Dummy Work Item For TfsEmbededImagesTool *****" ;
2527
2628 private Project _targetProject ;
27- private TfsTeamProjectEndpointOptions _targetConfig ;
2829
2930 private readonly IDictionary < string , string > _cachedUploadedUrisBySourceValue ;
3031
@@ -35,16 +36,23 @@ public TfsEmbededImagesTool(IOptions<TfsEmbededImagesToolOptions> options, IServ
3536 _cachedUploadedUrisBySourceValue = new System . Collections . Concurrent . ConcurrentDictionary < string , string > ( StringComparer . InvariantCultureIgnoreCase ) ;
3637 }
3738
38-
39- public int FixEmbededImages ( TfsProcessor processor , WorkItemData sourceWorkItem , WorkItemData targetWorkItem )
39+ public int FixEmbededImages ( TfsProcessor processor , WorkItemData targetWorkItem )
4040 {
4141 _processor = processor ;
4242 _targetProject = processor . Target . WorkItems . Project . ToProject ( ) ;
43- _targetConfig = processor . Target . Options ;
44- FixEmbededImages ( targetWorkItem , processor . Source . Options . Collection . AbsoluteUri , processor . Target . Options . Collection . AbsoluteUri , processor . Source . Options . Authentication . AccessToken ) ;
43+ string ? accessToken = processor . Source . Options . Authentication . AuthenticationMode switch
44+ {
45+ AuthenticationMode . AccessToken => processor . Source . Options . Authentication . AccessToken ,
46+ AuthenticationMode . Windows => GetWindowsAuthToken ( processor . Source . Options . Authentication . NetworkCredentials ) ,
47+ _ => null
48+ } ;
49+ FixEmbededImages ( targetWorkItem , processor . Source . Options . Collection . AbsoluteUri , processor . Target . Options . Collection . AbsoluteUri , accessToken ) ;
4550 return 0 ;
4651 }
4752
53+ private string GetWindowsAuthToken ( NetworkCredentials cred )
54+ => Convert . ToBase64String ( Encoding . ASCII . GetBytes ( $ "{ cred . Domain } \\ { cred . UserName } :{ cred . Password } ") ) ;
55+
4856 public void ProcessorExecutionEnd ( TfsProcessor processor )
4957 {
5058 _processor = processor ;
@@ -124,7 +132,7 @@ private string UploadedAndRetrieveAttachmentLinkUrl(string matchedSourceUri, str
124132 {
125133 if ( ! string . IsNullOrEmpty ( sourcePersonalAccessToken ) )
126134 {
127- httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Basic" , Convert . ToBase64String ( System . Text . Encoding . ASCII . GetBytes ( string . Format ( "{0}:{1}" , "" , sourcePersonalAccessToken ) ) ) ) ;
135+ httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Basic" , sourcePersonalAccessToken ) ;
128136 }
129137 var result = DownloadFile ( httpClient , matchedSourceUri , fullImageFilePath ) ;
130138 if ( ! result . IsSuccessStatusCode )
@@ -182,7 +190,7 @@ private Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.AttachmentRefere
182190 }
183191
184192 // Attaches it with dummy work item and removes it just to be able to make the image visible to all the users.
185- // VS402330: Unauthorized Read access to the attachment under the areas
193+ // VS402330: Unauthorized Read access to the attachment under the areas
186194 var payload = new Microsoft . VisualStudio . Services . WebApi . Patch . Json . JsonPatchDocument ( ) ;
187195 payload . Add ( new Microsoft . VisualStudio . Services . WebApi . Patch . Json . JsonPatchOperation ( )
188196 {
0 commit comments