如何获取在注册表中缓存的Visual Studio Online凭据?

时间:2013-12-27 23:24:52

标签: visual-studio tfs visual-studio-2013 tfsbuild azure-devops

当您登录Visual Studio 2013时,它会将您的个人资料和凭据缓存在注册表中:

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\ConnectedUser\IdeUser\Cache
HKEY_CURRENT_USER\Software\Microsoft\VSCommon\12.0\ClientServices\TokenStorage\VisualStudio\IdeUser

使用TFS API对Visual Studio Online进行身份验证时,它会将凭据复制到:

HKEY_CURRENT_USER\Software\Microsoft\VSCommon\12.0\ClientServices\TokenStorage\VisualStudio\VssApp

use tfs = new TfsTeamProjectCollection(Uri "https://ctaggart.visualstudio.com/DefaultCollection")
tfs.Authenticate()

如何使用Visual Studio或TFS API在C#或F#中使用这些值?

我认为Vss可能意味着Visual Studio服务。有一个Microsoft.VisualStudio.Services.Common。CredentialsCacheManager和另一个在Microsoft.TeamFoundation.Client中,但我不知道如何使用它们。它有一个ContainCredentials,GetCredentials和DeleteCredentials,因此看起来很有前途。 GetCredentials返回一个TfsCredentialCacheEntry,它有一个Credentials属性来获取System.Net.NetworkCredential,这正是我正在寻找的。

我不知道如何使用CredentialsCacheManager,但这是我尝试过的。

let ccm = Microsoft.TeamFoundation.Client.CredentialsCacheManager(@"Software\Microsoft\VSCommon\12.0\ClientServices\TokenStorage\VisualStudio", false)
ccm.ContainsCredentials("IdeUser", Uri "ideuser:https://app.vssps.visualstudio.com:vssuser:federated")

使用Process Monitor,它显示CredentialsCacheManager要么不是我想要的,要么我不知道如何使用它:

Process Monitor of Code

1 个答案:

答案 0 :(得分:2)

我明白了。 Microsoft.TeamFoundation.Client.TfsClientCredentialStorage有我正在寻找的东西。我把所有example code in a F# script as a gist都放了。我也会在这里复制它:

#r "Microsoft.TeamFoundation.Client"
#r "Microsoft.VisualStudio.Services.Common"
#r "System.Net.Http"

open System
open Microsoft.TeamFoundation.Client

// retrieve VssToken

// for the logged in user "IdeUser"
let vssTokenIdeUser = TfsClientCredentialStorage.RetrieveConnectedUserToken()

let tokenStorage = Microsoft.VisualStudio.Services.Common.TokenStorage.VssTokenStorageFactory.GetTokenStorageNamespace "VisualStudio"
let vssTokens = tokenStorage.RetrieveAll "VssApp" |> Array.ofSeq
for t in vssTokens do
    printfn "%s %s %s %s" t.Resource t.Type (t.GetProperty "UserId") (t.GetProperty "UserName")

// create a TfsClientCredentials by retrieving an IssuedToken

let ccs = TfsClientCredentialStorage()
let ct = ccs.RetrieveToken(Uri "https://ctaggart.visualstudio.com", Microsoft.VisualStudio.Services.Common.VssCredentialsType.Federated) :?> CookieToken
let cc = CookieCredential(false, ct)
let tcc = TfsClientCredentials cc

// use the TfsClientCredentials to authenticate with 

let tfs = new TfsTeamProjectCollection(Uri "https://ctaggart.visualstudio.com/DefaultCollection", tcc)
tfs.Authenticate()