Search code examples
c#tfsteam-project-collection

Connect to TfsTeamProjectCollection using WindowsCredentials


I want to connect to TfsProjectCollection using the same credentials I use to login to windows. Is this even possible?

I am connecting now with alternative credentials having this code:

NetworkCredential credential = new NetworkCredential(this._username, this._password);
VssBasicCredential basicCred = new VssBasicCredential(credential);
try
{
    _tfsDataConnection = new TfsTeamProjectCollection(new Uri(this._tfsLink), basicCred);

    // VssData Part

    Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    var url = new Uri(_tfsLink);

    VssCredentials vsc = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(
        new NetworkCredential(this._username, this._password)));
    VssConnection connection = new VssConnection(url, vsc);

    _vssDataConnection = connection.GetClient<BuildHttpClient>();
}

I will need to get Builds and Projects from that server.

This is what i tried but I get an error as like I am not authorized.

_tfsDataConnection = new TfsTeamProjectCollection(new Uri(this._tfsLink));
try
{
    VssCredentials vsc = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(CredentialCache.DefaultNetworkCredentials));
    VssConnection connection = new VssConnection(new Uri(_tfsLink), vsc);
    _vssDataConnection = connection.GetClient<BuildHttpClient>();
}

Solution

  • Do not pass any explicit credential: the classic client SDK will use the current user. _tfsDataConnection = new TfsTeamProjectCollection(new Uri(this._tfsLink)); _tfsDataConnection.Authenticate();

    The REST SDK is similar, you should use the default constructor. VssConnection connection = new VssConnection(new Uri(_tfsLink), new VssCredentials()); _vssDataConnection = connection.GetClient<BuildHttpClient>();