简体   繁体   中英

Team Foundation Client Not Using Credentials Provided

I am attempting to do some automated interaction with TFS Online (aka Visual Studio Online). While writing and testing the code on my development machine, this successfully connected:

creds = new Credentials(userName, password);

tfsConfig = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(vsoUrl), creds);
tfsConfig.EnsureAuthenticated();

Credentials Class:

public class Credentials : ICredentialsProvider
{
    string _userName;
    string _password;

    public Credentials(string userName, string password)
    {
        _userName = userName;
        _password = password;
    }

    public ICredentials GetCredentials(Uri uri, ICredentials failedCredentials)
    {
        return new NetworkCredential(_userName, _password);
    }

    public void NotifyCredentialsAuthenticated(Uri uri)
    {
        throw new NotImplementedException();
    }
}

I then went to deploy the code and run it under the service account, I received this error:

Microsoft.TeamFoundation.TeamFoundationAuthenticationRedirectionException: TF30064: You are not authorized to access the server.

at

Microsoft.TeamFoundation.Client.TfsConnection.ThrowAuthorizationException(Exception e)

at

Microsoft.TeamFoundation.Client.TfsConnection.UseCredentialsProviderOnFailure(Action action, Int32 retries, Boolean throwOnFailure)...

So after a lot of attempted solutions, I tried passing in an incorrect 'password' on my local development machine test and it still connected fine! So I can only assume I am not setting the credentials properly in code and the successful local tests are because I am already connected in Visual Studio.

Can anyone make sense of this or spot what I am doing wrong?

I think the reason it works on your dev machine is because your valid creadentials has been cached already so your CredentialsProvider is not called at all.

TfsConfigurationServer constructor taking ICredentialsProvider is marked as obsolete in MSDN documentation . I would recommend using the one which takes ICredentials as an input ( here ). The code will look like this:

var userCreds = new NetworkCredential(userName, password);
var tfsServer = new TfsConfigurationServer(new Uri(vsoUrl), userCreds);
tfsServer.EnsureAuthenticated();

Another option would be to use TfsTeamProjectCollection class instead of TfsConfigurationServer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM