none
How to connect to TFS Online programmatically with non-service user account RRS feed

  • Question

  • Hi there,

    What is the correct way to connect to a TFS project collection hosted in Azure as a specific user i.e. NOT as a service account without any UI prompt?

    I tried the suggested code below (from another forum post) but it does not work.

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

    namespace SourceControlTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    var netCred = new NetworkCredential(@"MyVS2017UserName", @"MyVS2017Password");
                    var winCred = new Microsoft.VisualStudio.Services.Common.WindowsCredential(netCred);
                    var vssCred = new VssCredentials(winCred);
                    var tpc = new TfsTeamProjectCollection(new Uri("https://ourprojectcollectionname.visualstudio.com"), vssCred);
                    tpc.Authenticate();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                Console.ReadLine();
            }
        }
    }

    I get the exception:

    Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException: TF30063: You are not authorized to access https://x.visualstudio.com/. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
       at System.Net.HttpWebRequest.GetResponse()
       at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequestAndGetResponse(HttpWebRequest webRequest, WebException& webException)
       --- End of inner exception stack trace ---
       at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequest()
       at Microsoft.TeamFoundation.Client.Channels.TfsHttpRequestChannel.Request(TfsMessage message, TimeSpan timeout)
       at Microsoft.TeamFoundation.Client.Channels.TfsHttpClientBase.Invoke(TfsClientOperation operation, Object[] parameters, TimeSpan timeout, Object[]& outputs)
       at Microsoft.TeamFoundation.Framework.Client.LocationWebService.Connect(Int32 connectOptions, Int32 lastChangeId, Int32 features)
       at Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Connect(ConnectOptions connectOptions)
       at Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Authenticate()
       at Microsoft.TeamFoundation.Client.TfsConnection.Authenticate()
       at SourceControlTest.Program.Main(String[] args) in C:\SourceControl\X.Deployment\Source\BuildProcess\SourceControlTest\Program.cs:line 18

    Would someone be able to help me figure out what the issue is...?

    I did try and find a TFS specific forum but they all appear to be archived...  Please feel free to move this question to a more suitable location...







    Wednesday, January 3, 2018 1:14 PM

All replies

  • Hi jeanangeo,

    According to your description, you want to connect to TFS Online programmatically.

    Could you please use this method and have a test:

    using Microsoft.TeamFoundation.Client;

    using Microsoft.VisualStudio.Services.Client;

    var u = new Uri("https://{your account}.visualstudio.com");

    //VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "juwlupdpprcwqqonrpr7h3qnerozvin2oaokmcfsl22bpqtlhcda"));

    var c = new VssClientCredentials();

    c.Storage = new VssClientCredentialStorage(storageKind: "VssApp2", storageNamespace: "VisualStudio");

    TfsTeamProjectCollection _tfs = new TfsTeamProjectCollection(u, c);

    _tfs.EnsureAuthenticated();

    Above method will eject the landing box login, if you use the comment line, it need to specify personal access token or alternate account.

    >> I did try and find a TFS specific forum but they all appear to be archived...  Please feel free to move this question to a more suitable location...

    Since the TFS forums have been locked, the discussion for TFS has moved. I recommend you post your issue on Stack Overflow, and then you would get support from TFS members.

    Regards,

    Judyzh


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 4, 2018 6:24 AM
  • Hi Judyzh,

    Thank you very much for taking the time to reply.  It is very much appreciated.

    Are you aware of any documentation for all the various credentials types?  During my attempts to work out how to connect properly I have come across numerous examples that all use different credentials types.  Are you aware of any documentation (apart from archive and non-maintained MS pages)?

    For example, when I google the type VssClientCredentialStorage the only pages that come up are from VS 2013 and are no longer maintained.  This has been the case for most of the types I have tried to research.  What is the purpose of this type?  The page only says it add token storage support but this means nothing to me.

    I'll try the code and see if it solves my problem anyway but I was hoping I could find some reference material on the subject in general.

    Kind regards,

    jeanangeo


    Thursday, January 4, 2018 8:46 AM
  • Hi jeanangeo,

    >>Are you aware of any documentation for all the various credentials types?

    I searched and found the following document, hope could help you.

    https://docs.microsoft.com/en-us/vsts/integrate/concepts/dotnet-client-libraries

    https://blog.joergbattermann.com/2016/04/13/vsts-tfs-rest-api-03-authentication/

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites;

    Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards,

    Judyzh


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Judy ZhuY Monday, January 8, 2018 2:06 AM
    • Unproposed as answer by Judy ZhuY Thursday, January 11, 2018 9:15 AM
    • Proposed as answer by Judy ZhuY Thursday, January 11, 2018 9:15 AM
    Friday, January 5, 2018 2:05 AM
  • Hi jeanangeo,

    If any reply helped you to resolve your issue or doubts, please help us to mark it as answer then I will close this thread. If the issue still exists please post the latest result of it, we will try our best to help you out.

    Thanks for your understanding. 

    Regards,

    Judyzh


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 15, 2018 8:42 AM