TFS api not validating the credentials

大城市里の小女人 提交于 2019-12-13 06:28:41

问题


I have the following code on my windows form application:

        // Connect to server
        var tfs = new TeamFoundationServer(tfsServer, credentials);
        try
        {
            tfs.EnsureAuthenticated();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        };

This is working. The problem is TeamFoundationServer is an obsolete class.

The TeamFoundationServer class is obsolete. Use the TfsTeamProjectCollection or TfsConfigurationServer classes to talk to a 2010 Team Foundation Server. In order to talk to a 2005 or 2008 Team Foundation Server use the TfsTeamProjectCollection class.

I am using the below code now but it doesn't validate my credentials. What did I do wrong here?

        NetworkCredential credentials = new NetworkCredential(username, password, domain);

        MyCredentials credentials = new MyCredentials(username, password, domain);
        TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer), credentials);
        try
        {
            tfs.EnsureAuthenticated();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

This is the MyCrendentials class:

    private 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)
        {
            throw new NotImplementedException();
        }

        #endregion
     }

Thanks in advance!


回答1:


I struggled with the same thing this morning. You don't need any of that interface/implementation code anymore. This is how I got it to work:

// Translate username and password to TFS Credentials
ICredentials networkCredential = new NetworkCredential(tfsUsername, tfsPassword, domain);
WindowsCredential windowsCredential = new WindowsCredential(networkCredential);
TfsClientCredentials tfsCredential = new TfsClientCredentials(windowsCredential, false);

// Connect to TFS Work Item Store
Uri tfsUri = new Uri(@"http://my-server:8080/tfs/DefaultCollection");
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, tfsCredential);
WorkItemStore witStore = new WorkItemStore(tfs);



回答2:


I know it's an old question, but I was asking the same question at one point.

NetworkCredential credentials = NetworkCredential("User", "Password", "Domain");
TfsConfigurationServer tfs = new TfsConfigurationServer(new Uri("Address"), credentials);
tfs.Authenticate();



回答3:


A detailed walkthrough is here.



来源:https://stackoverflow.com/questions/21668276/tfs-api-not-validating-the-credentials

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!