简体   繁体   中英

Authentication to TFS Service fails from WinForms in VS2010

In my .Net windows application (a code generator) I want to check out files programmaticaly from a TFS service. Although I can access and checkout files in VS2010 from the TFS Service without any problems, my program always returns

"TF30064: You are not authorized to access the server"

I have tried numerous things, something like this:

var account = new NetworkCredential(Username, Password);
tfs.TfsTeamProjectCollection teamProjectCollection = 
    new tfs.TfsTeamProjectCollection(new Uri(anUrl), account);
teamProjectCollection.Authenticate();

Any help is much appreciated

If you are running you application as yourself, and you have access to TFS, then you can just do:

var uri = new Uri("http://tfsserver:8080/tfs/MyCollection");
using (var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(uri))     
{         
    tfs.EnsureAuthenticated(); 
    //Your code goes here...
}

Take a look at: Connect to Team Foundation Server from a Console Application .

It says about a console application, but you can use all tips in your WinForm app as well.

In special, this part in (Acting on Behalf of Another User (Impersonation)):

Using Authenticated Credentials

You can use an ICredentials object when you connect to Team Foundation Server to specify the identity to impersonate. This strategy does not require special permissions, but you must be able to obtain the password of the identity to create the ICredentials object. You can also specify an implementation of ICredentialsProvider when you connect to Team Foundation Server to handle requests for new credentials. The system calls the implementation of ICredentialsProvider that you specify to request new credentials when the credentials that are specified by the ICredentials object are not successfully authenticated or authorized to perform the operation. To prompt the user for credentials, you can use the UICredentialsProvider class, which implements ICredentialsProvider by displaying a logon dialog box to prompt the user for new credentials.

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