1

I am trying to access TFS server via c# code and I keep running in this issue. The issue occurs only when I try to debug the code but if I just try to build and run it succeeds.

var credentials = new NetworkCredential("username","password");
var projects = new TfsTeamProjectCollection(new Uri("http://xxx-xxxx.com:8080/tfs/xxxxx"), credentials);
    projects.EnsureAuthenticated();

    if (Directory.Exists(localPath))
    {
         foreach (var item in Directory.GetFiles(localPath, "*.*", SearchOption.AllDirectories))
         {
             File.SetAttributes(item, FileAttributes.Normal);
         }
             Directory.Delete(localPath, true);
    }


WorkingFolder[] mapping = serverItem.Select(x => new WorkingFolder(x, Path.Combine(localPath, GetDummyString(x)))).ToArray();

var workspace = versionControl.CreateWorkspace(workspaceName, Environment.UserName, Environment.UserName + " Checkout", mapping);
workspace.Get();

It happens is on projects.EnsureAuthenticated(); call. I get:

System.Net.WebException occurred:
  HResult=-2146233079
  Message=The remote server returned an error: (401) Unauthorized.
  Source=System
  StackTrace: at System.Net.HttpWebRequest.GetResponse()
  InnerException: null

It only occurs when I am trying to debug, IF I comment that line out I get a new webException saying: Additional information: The remote server returned an error: (401) Unauthorized. from the call to versionControl.CreateWorkspace()

UPDATE: What I noticed is when it throws an exception while debugging, if I try to continue (clicking F10 multiple times) the exception disappears and code succeeds.

UPDATE 2: After struggling with this issue for a while it seems the issue why I am getting this error is because some of the properties do not get initialized when I create an object. As you can see in the picture: Link here

As you can see from the picture what fails to get initialized are AuthorizedIdentity and CatalogNode. Any idea how could I solve it?

Min0
  • 150
  • 1
  • 1
  • 12
  • Is this in the context of an ASP.NET web app, by any chance? – mars-red Apr 05 '16 at 14:09
  • No, I'm using desktop console app. I am trying to make a connection to tfs via console app which will be later made as a class library. – Min0 Apr 05 '16 at 14:10
  • You're getting an `HTTP 401` response which means that the request has not been applied because it lacks valid authentication credentials for the target resource. I see you're not adding a NetworkCredential anywhere so I'd suggest adding on to `projects` if you can. Otherwise I'm not quite sure where you should add it but that'd be a way to go anyway – Ortund Apr 05 '16 at 14:14
  • I will try that, though I know that gives me a different exception (have to replicate it to get exception again). On another note, there is still question - why is it only when debugging if credentials are missing? – Min0 Apr 05 '16 at 14:18
  • I changed the code above to: `var credentials = new NetworkCredential("UserName","Password"); var projects = new TfsTeamProjectCollection(new Uri("http://xxx-xxxxxx.com:8080/tfs/xxxxxxx"), credentials);` but it is still giving the same exception. – Min0 Apr 05 '16 at 14:21
  • The answer here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/b6d8c69f-655f-4391-bda1-f3fefa7d54f3/teamfoundationserverensureauthenticated-appears-to-hang-in-aspnet-app?forum=tfsgeneral States that the EnsureAuthenticated method will trigger a winforms prompt for authentication, I wonder if part of your problem is using that in the context of a console application. By default, it should be trying to use the credentials of whatever account the process is running as. Perhaps visual studio is running in a different user context than when you run the app without the debugger attached? – mars-red Apr 05 '16 at 14:28
  • I doubt because either when I am debugging on when I run it is using my windows authentication credentials which are the same for tfs server. Plus I when I create a TfsTeamproejctcollection I can see that I do retrieve information from the tfs server... I will look into the link you attached to see if it can give me some input.. – Min0 Apr 05 '16 at 14:36
  • Does your VS connected to another TFS server? Reference for you: http://stackoverflow.com/questions/34490310/vs-remembers-tfs-credentials-even-after-clearing-cache-and-deleting-from-credent – Eddie Chen - MSFT Apr 08 '16 at 08:25

1 Answers1

1

Add domain string will get rid of this issue:

  var credentials = new NetworkCredential("Username", "Password", "Domain");
  var projects = new TfsTeamProjectCollection(new Uri("http://tfsserver:8080/tfs/teamprojectcollection"), credentials);
  projects.EnsureAuthenticated();
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • for the domain can I use `Environment.UserDomainName` ? If yes, that does not fix the issue, because when I add it and try to debug it gives me exception on the next step `projects.EnsureAuthenticated()` (same webException: the remote server returned an error: (401) Unauthorized) – Min0 Apr 06 '16 at 07:03
  • I have tried var credentials = new NetworkCredential("Domain\Username", "Password"), but doesn't work. Using var credentials = new NetworkCredential("Username", "Password", "Domain") works. – Cece Dong - MSFT Apr 06 '16 at 07:08
  • are you trying to run the code or debug? I am executing this code snippet though unit test project and if I just try to run, it all green, but if I try to debug, it always crashes on `EnsureAuthenticated` call... – Min0 Apr 06 '16 at 07:12
  • actually what I noticed is when it throws an exception, if I try to continue (F10 multiple times) the exception disappears and code succeeds and continues.. – Min0 Apr 06 '16 at 07:17
  • I debug the code. If I use var credentials = new NetworkCredential("Domain\Username", "Password"), I'll get exception "TeamFoundationServerUnauthorizedException was unhandled", and information "TF30063: You are not authorized to access http://tfsserver:8080/tfs/teamprojectcollection". If I use var credentials = new NetworkCredential("Username", "Password", "Domain"), no exception or information show up. By the way, I use VS 2015 Update1 + TFS 2015 Update2. – Cece Dong - MSFT Apr 06 '16 at 07:28
  • Hey, I think that this is not the underlying issue for me. What I can see from the snippet behavior is that it succeeds if I retry it multiple times, which suggests (in my mind) that I have access denied only temporarily until it gets released and I can connect, but since it throws an exception instead of retrying it doesn't connect – Min0 Apr 06 '16 at 11:29
  • I have updated the question with some new discoveries that might help to figure what is causing this issue and how to handle it// – Min0 Apr 06 '16 at 13:18
  • Can't reproduce your issue. Which version of TFS are you using? Do you use proxy? Why the collection has .com (http://xxx-xxxx.com:8080/tfs/xxxxx) in your code? – Cece Dong - MSFT Apr 07 '16 at 05:41
  • it's .com because my domain is hosted there. and about versions information: Microsoft Visual Studio Enterprise 2015, Version 14.0.25123.00 Update 2, Microsoft .NET Framework Version 4.6.01038; Microsoft Team Foundation Server 2015 Power Tools 14.0. I was looking for specific version of TFS but this is all I could get from VS... And no, I'm using any proxy.. A this point I think I reached the root of the issue and that is `"The function evaluation requires all thread to run"` – Min0 Apr 07 '16 at 06:41
  • These are client software versions, not TFS. Do you use on-premise TFS or VSTS (https://xx.visualstudio.com/)? – Cece Dong - MSFT Apr 07 '16 at 08:49
  • I am using VSTS version – Min0 Apr 07 '16 at 10:12
  • That's the issue. You are using wrong URL. Try using https:// xxx.visualstudio.com/DefaultCollection/ in your code. – Cece Dong - MSFT Apr 07 '16 at 10:23
  • ok, I think then I confused that VSTS is... sorry... This is how I see it in visual studio. I go to TeamExplorer and in there in "Manage Connections" I can see the link to my team foundation server. In my case it is "tfs-some.something.com" the way I translate this to the uri I use for TfsTeamProjectCollection() parameter is "http://tfs-some.something.com:8080/tfs/MyProject" and I wrong? – Min0 Apr 07 '16 at 12:49
  • If you use on-premise TFS, the URL is: http:// tfsservername:8080/tfs/teamprojectcollectionname, if you use VSTS, the URL is: https:// xxx.visualstudio.com/DefaultCollection. – Cece Dong - MSFT Apr 08 '16 at 01:53