Search code examples
tfstfsbuildtfs-sdk

Input directly user info when query work items


When code run into line 2, Windows will show a popup to input username, password for TFS access:

TeamProjectCollection = new TfsTeamProjectCollection(new Uri(url), new UICredentialsProvider());
TeamProjectCollection.Connect(Microsoft.TeamFoundation.Framework.Common.ConnectOptions.IncludeServices);
TeamProjectCollection.EnsureAuthenticated();
IsConnected = true;

Have any way to assign username, password automatically without login from popup of windows.

Thanks.


Solution

  • If you want to directly insert user credentials and not use the one the process is running, the following worked for me:

    WindowsCredential credentials = new WindowsCredential(new NetworkCredential(username, domain, password), new MyCredentials(username, domain, password));
    TfsTeamProjectCollection connectedTPC = new TfsTeamProjectCollection(tfsUrl, new TfsClientCredentials(credentials));
    

    It's a bit strange to provide credentials two times, but without the MyCredentials I will get no answer from TFS, not sure why.

    public class MyCredentials : ICredentialsProvider
    {
        private NetworkCredential credentials;
        #region ICredentialsProvider Members
        public MyCredentials(string user, string domain, string password)
        {
            credentials = new NetworkCredential(user, password, domain);
        }
    
        public ICredentials GetCredentials(Uri uri, ICredentials failedCredentials)
        {
            return credentials;
        }
    
        public void NotifyCredentialsAuthenticated(Uri uri)
        {
            // who cares
        }
    
        #endregion
    }