I'm having trouble authenticating as a specific user on MS Team Foundation Server. In older versions it would look like:
teamFoundationCredential = new System.Net.NetworkCredential("<USERNAME>", "<PASSWORD>", "<DOMAIN>");
TeamFoundationServer tfs = new TeamFoundationServer("http://mars:8080/", teamFoundationCredential);
Can some one tell me the 开发者_Go百科equivilent for the 2010 version. So far I have:
ICredentialsProvider cred = null;
tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://asebeast.cpsc.ucalgar.ca:8080/tfs/DefualtCollection"));
tfs.EnsureAuthenticated();
Thanks
For TFS 2010, use the following:
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(
new Uri("http://asebeast.cpsc.ucalgar.ca:8080/tfs/DefaultCollection",
new System.Net.NetworkCredential("domain_name\\user_name", "pwd"));
collection.EnsureAuthenticated();
I've been having the same problem. The above solution doesn't work for me and really can't figure out why. I keep getting a cast exception. Spent a day trying to figure this out - so thought I'd share my current workaround to the problem. I've created my own internal class that implements ICredentialsProvider - as below:
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
}
I then instantiate this and pass it in as below:
MyCredentials credentials = new MyCredentials(UserName, Password, Domain);
TfsTeamProjectCollection configurationServer =
TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
new Uri(tfsUri), credentials);
Note that I haven't implemented the NotifyCredentialsAuthenticated - not sure what this actually does, so left the NotImplementedException in there so I could catch when its called, which so far hasn't happened. Now successfully connected to TFS.
I've had some problems connecting to our old TFS 2008 server using this method as well, but the thing that solved my case was really simple:
First I defined the TFS Url to be:
private const string Tfs2008Url = @"http://servername:8080/tfs/";
static readonly Uri Tfs2008Uri = new Uri(Tfs2008Url);
The path used in the URL is the one we use when connecting via VisualStudio, so I thought this had to be the same in API calls, but when I tried to use this with the following authentication, I got a TF31002 / 404 error:
var collection = new TfsTeamProjectCollection(Tfs2008Uri,new NetworkCredential("AdminUser","password","domain_name"));
collection.EnsureAuthenticated();
But when I changed the Url to the TFS root, it authenticated OK!
private const string Tfs2008Url = @"http://servername:8080/";
static readonly Uri Tfs2008Uri = new Uri(Tfs2008Url);
Don't know if that helped anyone, but it sure did the trick for me!
This has worked pretty good for me:
_tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);
_tfs.ClientCredentials = new TfsClientCredentials(new WindowsCredential(new NetworkCredential("myUserName", "qwerty_pwd", "myDomainName")));
_tfs.EnsureAuthenticated();
精彩评论