2

I am using TFS 15.x. package.

Error:

Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException: 'TF30063: You are not authorized to access "https://myproject.visualstudio.com/RpaCodeReview'

Uri Repurl = new Uri("https://myproject.visualstudio.com/RpaCodeReview");
NetworkCredential netCred = new NetworkCredential(username, password);
VssBasicCredential basicCred = new VssBasicCredential(netCred);
VssCredentials tfsCred = new VssCredentials(basicCred);
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(Repurl, tfsCred);
tpc.EnsureAuthenticated();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sinton k
  • 23
  • 6
  • 2
    Maybe you are "not autorized to access" the tfs? – Henrik Wilshusen Jan 17 '19 at 10:18
  • I have authorization to the project – Sinton k Jan 17 '19 at 10:19
  • I had issues as well for this and eventually i switched to using TFS rest apis for commmunication with tfs when they released them and since i do not have issues with maintaining correct library versions when upgrading tfs. If you want tell me and i will post the how for starters. – markorial Jan 17 '19 at 10:39
  • can you please share me the code using tfs apis. – Sinton k Jan 17 '19 at 10:59
  • The REST API are pretty easy to work with. Here is the [documentation](https://learn.microsoft.com/en-us/azure/devops/integrate/previous-apis/overview?view=tfs-2017) for VS2017. Here's an [example](https://stackoverflow.com/questions/50483072/how-to-queue-a-new-build-using-vsts-rest-api) for putting a new build in queue. – Matt Jan 17 '19 at 22:02

2 Answers2

3

It depends on the version of your TFS. However, if you're trying to connect to TFS2015, or TFS2017, this will do;

using Microsoft.TeamFoundation.Client;
using Microsoft.VisualStudio.Services.Common;
using System;
using System.Net;   

namespace TFSConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            NetworkCredential networkCredentials = new NetworkCredential(@"Domain\Account", @"Password");
            Microsoft.VisualStudio.Services.Common.WindowsCredential windowsCredentials = new Microsoft.VisualStudio.Services.Common.WindowsCredential(networkCredentials);
            VssCredentials basicCredentials = new VssCredentials(windowsCredentials);
            TfsTeamProjectCollection tfsColl = new TfsTeamProjectCollection(
                new Uri("http://XXX:8080/tfs/DefaultCollection"),
                basicCredentials);

            tfsColl.Authenticate(); // make sure it is authenticate
        }
    }
}

I cannot stress enough to ensure the credentials are a-okay! This error has occured to me a couple times too.

There is also another solution if the above doesn't work.

  1. Close Visual Studio and go to Control Panel
  2. User Accounts --> Manage your Credentials (on the left column)
  3. Select "Windows Credentials"
  4. Scroll down to the "Generic Credentials" section and look for your TFS server connection
  5. Expand the pull down and click "Edit"
  6. Enter in your network password
  7. Restart Visual Studio and retry the code
foyss
  • 973
  • 2
  • 8
  • 24
  • This is using the windows credentials right. Can I use the Microsoft credentials? – Sinton k Jan 17 '19 at 11:02
  • When you go down to Generic Credentials and find your TFS connection, just edit and re-enter your password. It should be the password you used for TFS – foyss Jan 17 '19 at 11:08
  • If that doesn't work, try deleting your TFS credentials and re-adding it. Let me know how it goes – foyss Jan 17 '19 at 11:09
  • Can you try putting admin credentials in your program and see if you still get the same error? – foyss Jan 17 '19 at 12:23
  • 1
    Its working now. I changed the credentials in Microsoft account. Thanks – Sinton k Jan 17 '19 at 12:43
0

Along with all the comments on credentials I have found basic authentication blocked on some repositories.

I have found it best to create Personal Access Token (PAT) in the repository. Then use that in you connections to access the APIs.

Example to read what projects are in the default collection of a tfs/devops repo:

string PAT = "Put PAT String Here";
string RepoStore = "https://url of repo here";
string responseBody = "";

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(
        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
        Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}", "", PAT))));

    using (HttpResponseMessage response = client.GetAsync(
                RepoStore + "/_apis/projects").Result)
    {
        response.EnsureSuccessStatusCode();
        responseBody = await response.Content.ReadAsStringAsync();
    }
    Console.WriteLine(responseBody);
}

Console.ReadKey();
C J
  • 144
  • 8