Question

I have a limited access to the TFS server,so I intended to use a administrator account in the code to connect to the server.I found that when I deploy the service on a computer which does not join into the domain,it works just fine,but when deploy the service on a domain-joined computer,it uses integrated credential.Great thanks for help!Following is my code.

        Uri tfsUri = Common.Helper.GetCollectionUri();
        UserCredential userCredential = new UserCredential("administrator", "pass@word1", "zanewill.net");

        TfsTeamProjectCollection col = new TfsTeamProjectCollection(tfsUri, userCredential);

        TeamFoundationIdentity identity;
        col.EnsureAuthenticated();
        col.GetAuthenticatedIdentity(out identity);

I want the credential of administrator,but the identity always be the credential of 'zanewill' which is a limited domain account.

the class of UserCredential is like following public class UserCredential : ICredentialsProvider { private NetworkCredential networkCredential;

    public UserCredential(string userName, string password, string domain) {
        networkCredential = new NetworkCredential(userName, password, domain);

    }

    public System.Net.ICredentials GetCredentials(Uri uri, System.Net.ICredentials myCredentials) {
        return networkCredential;
    }

    public void NotifyCredentialsAuthenticated(Uri uri) {

    }
}

I use NetworkCredential like the following code, and it works, I don't know why.

        Uri tfsUri = Common.Helper.GetCollectionUri();
        var networkCredential = new NetworkCredential("administrator", "pass@word1", "zanewill.com");

        TfsTeamProjectCollection col = new TfsTeamProjectCollection(tfsUri,networkCredential);
        TeamFoundationIdentity identity;
        col.EnsureAuthenticated();
        col.GetAuthenticatedIdentity(out identity);
Was it helpful?

Solution

Here's why:

In your first piece of code, you used the following constructor of TfsTeamProjectCollection:

public TfsTeamProjectCollection(
    Uri uri,
    ICredentialsProvider credentialsProvider
)

And the second parameter is defined as:

Used to get the credentials when the credentials of the current environment fail.

So if the credentials of the current environment (aka the limited domain account you mentioned) do not fail, it will use that instead of the Administrator account you created the ICredentialsProvider with. Did you log on as zanewill when you tried this code?

The second constructor you used takes the second parameter as the "authentication credentials" so it's the one being used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top