AJAX cross domain issue with Visual Studio Team Services REST API

安稳与你 提交于 2019-12-03 07:36:19

Somebody at Microsoft finally gave me the solution, so here it is:

In PowerShell, run these commands:

[Reflection.Assembly]::LoadFrom("C:\Program Files\Microsoft Team Foundation Server 14.0\Tools\Microsoft.TeamFoundation.Client.dll")

$configServer = new-object Microsoft.TeamFoundation.Client.TfsConfigurationServer "http://localhost:8080/tfs/"

$configHive = $configServer.GetService([Microsoft.TeamFoundation.Framework.Client.ITeamFoundationRegistry])

$configHive.SetValue("/Configuration/WebSecurity/AllowedOrigins", "domain1;domain2")

So you can specify several domains and you can also restrict to a given port and/or scheme like this:

$configHive.SetValue("/Configuration/WebSecurity/AllowedOrigins", "localhost,port=58785,scheme=http;")

Here is an old blog post about Updating the TF Registry using Powershell

Then you can finally send authenticated AJAX requests to the API.

[EDIT]: At this point, if you are running it in Windows it may be working, however it doesn't use Basic Authentication.

Two options:

1. It uses Generic Credentials automatically added in the Credential Manager (Sorry it's in French)

2. Or it could also use your Windows session credentials.

So to get it working in a non-Windows environment, you still need few more steps.

On your TFS server, run this PowerShell command to add Basic Authentication feature:

dism /online /enable-feature /featurename:IIS-BasicAuthentication

Then in IIS Manager click "Authentication" on your TFS site node. You should now see Basic Authentication, just enable it.

Finally in your JavaScript code convert the string

DOMAIN\username:password

to Base64 and add it to the request's header (assuming you use XMLHttpRequest):

client.setRequestHeader('Authorization', 'Basic ' + myBase64AuthString);

NOTE: Be careful with the pure JavaScript Base64 converter you may find on internet. The converted string could be wrong due to encoding. Compare your string with some online Base64 converters to be sure.

Hope this will help other people.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!