{"id":233,"date":"2013-01-07T07:09:00","date_gmt":"2013-01-07T07:09:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/buckh\/2013\/01\/07\/how-to-connect-to-tf-service-without-a-prompt-for-liveid-credentials\/"},"modified":"2013-01-07T07:09:00","modified_gmt":"2013-01-07T07:09:00","slug":"how-to-connect-to-tf-service-without-a-prompt-for-liveid-credentials","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/buckh\/how-to-connect-to-tf-service-without-a-prompt-for-liveid-credentials\/","title":{"rendered":"How to connect to TF Service without a prompt for LiveID credentials"},"content":{"rendered":"<p>Normally when you connect to the <a href=\"http:\/\/tfs.visualstudio.com\">Team Foundation Service<\/a> you are presented with a web page to log in with your Microsoft Account (aka LiveID). When you log in you can choose to have it remember you and you won\u2019t have to enter your Microsoft Account credentials again (unless you don\u2019t log in again for a long time, and then you\u2019ll be prompted again).<\/p>\n<p>That\u2019s great for humans, but what about an application or another web service that wants to connect? For that the code will need to use \u201calternate credentials,&quot; which you must enable on your account. This is the same setting used to enable <a href=\"http:\/\/tfs.visualstudio.com\/en-us\/learn\/code\/use-git-and-vs-with-tfs\/\">basic authentication for git-tf<\/a>. Then we can write some code to connect to the service with those credentials.<\/p>\n<p>Longer term, we will have OAuth support available as well, but that\u2019s not ready yet.<\/p>\n<p><strong>Enabling Alternate Credentials<\/strong><\/p>\n<p>You\u2019ll need to first to turn on this feature. First, visit your account or project in a browser, click on your name in the upper right, and then click My Profile.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/0880.myprofile_494ABDAB.png\"><img decoding=\"async\" title=\"myprofile\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;padding-top: 0px;padding-left: 0px;margin: 0px 0px 0px 10px;padding-right: 0px;border-top-width: 0px\" border=\"0\" alt=\"myprofile\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/1856.myprofile_thumb_0EEF47CA.png\" width=\"154\" height=\"112\" \/><\/a><\/p>\n<p>On the User Profile dialog, click on the Credentials tab.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/8132.enablecreds_6748CB9F.png\"><img decoding=\"async\" title=\"enablecreds\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;padding-top: 0px;padding-left: 0px;margin: 0px 0px 0px 10px;padding-right: 0px;border-top-width: 0px\" border=\"0\" alt=\"enablecreds\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/3426.enablecreds_thumb_667065B5.png\" width=\"504\" height=\"421\" \/><\/a><\/p>\n<p>Now provide a password and save the changes.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/3835.password_2B3C89EA.png\"><img decoding=\"async\" title=\"password\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;padding-top: 0px;padding-left: 0px;margin: 0px 0px 0px 10px;padding-right: 0px;border-top-width: 0px\" border=\"0\" alt=\"password\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/0218.password_thumb_1C91DE05.png\" width=\"504\" height=\"423\" \/><\/a><\/p>\n<p><strong>Using Alternate Credentials in code<\/strong><\/p>\n<p>Before going further, you\u2019ll need to make sure that you have Update 1 for Visual Studio 2012 or newer installed. That update includes enhancements to the TFS client object model to support alternate credentials.<\/p>\n<p>The easiest way to get the latest update is either via clicking on the \u201ctoast\u201d notification that pops up from the Windows taskbar or in VS going to Tools \u2013&gt; Extensions and Updates\u2026, clicking on Updates followed by Product Updates and installing the latest update. Alternatively, you can download it <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=35774\">here<\/a>.<\/p>\n<p>You can verify that you have Update 1 (or newer) installed in VS using Help \u2013&gt; About Microsoft Visual Studio.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/5187.vshelp_2FD294A4.png\"><img decoding=\"async\" title=\"vshelp\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;padding-top: 0px;padding-left: 0px;margin: 0px 0px 0px 10px;padding-right: 0px;border-top-width: 0px\" border=\"0\" alt=\"vshelp\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/4024.vshelp_thumb_68110BBC.png\" width=\"504\" height=\"377\" \/><\/a><\/p>\n<p>Now that we have the credentials turned on, we\u2019ll now use them from a simple console app.<\/p>\n<pre class=\"code\">&#160;<\/pre>\n<p>After creating a new console app, add a reference to Microsoft.TeamFoundation.Client.dll, which you will find under v2.0 in ReferenceAssemblies. The client object model for TFS is almost entirely built with .NET 3.5 (CLR 2.0) in order to support running the TFS web parts in SharePoint.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/5504.image_5F410370.png\"><img decoding=\"async\" title=\"image\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;padding-top: 0px;padding-left: 0px;margin: 0px 0px 0px 10px;padding-right: 0px;border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/5504.image_thumb_6443411F.png\" width=\"504\" height=\"331\" \/><\/a><\/p>\n<p>Here\u2019s the code.<\/p>\n<pre class=\"code\"><span style=\"background: white;color: blue\">using <\/span><span style=\"background: white;color: black\">System;\n<\/span><span style=\"background: white;color: blue\">using <\/span><span style=\"background: white;color: black\">System.Collections.Generic;\n<\/span><span style=\"background: white;color: blue\">using <\/span><span style=\"background: white;color: black\">System.Linq;\n<\/span><span style=\"background: white;color: blue\">using <\/span><span style=\"background: white;color: black\">System.Net;\n<\/span><span style=\"background: white;color: blue\">using <\/span><span style=\"background: white;color: black\">System.Text;\n<\/span><span style=\"background: white;color: blue\">using <\/span><span style=\"background: white;color: black\">System.Threading.Tasks;\n\n<\/span><span style=\"background: white;color: blue\">using <\/span><span style=\"background: white;color: black\">Microsoft.TeamFoundation.Client;\n\n<\/span><span style=\"background: white;color: blue\">namespace <\/span><span style=\"background: white;color: black\">ConsoleApplication1\n{\n    <\/span><span style=\"background: white;color: blue\">class <\/span><span style=\"background: white;color: #2b91af\">Program\n    <\/span><span style=\"background: white;color: black\">{\n        <\/span><span style=\"background: white;color: blue\">static void <\/span><span style=\"background: white;color: black\">Main(<\/span><span style=\"background: white;color: blue\">string<\/span><span style=\"background: white;color: black\">[] args)\n        {\n            <\/span><span style=\"background: white;color: #2b91af\">NetworkCredential <\/span><span style=\"background: white;color: black\">netCred = <\/span><span style=\"background: white;color: blue\">new <\/span><span style=\"background: white;color: #2b91af\">NetworkCredential<\/span><span style=\"background: white;color: black\">(\n                <\/span><span style=\"background: white;color: #a31515\">&quot;someone@yahoo.com&quot;<\/span><span style=\"background: white;color: black\">,\n                <\/span><span style=\"background: white;color: #a31515\">&quot;password&quot;<\/span><span style=\"background: white;color: black\">);\n            <\/span><span style=\"background: white;color: #2b91af\">BasicAuthCredential <\/span><span style=\"background: white;color: black\">basicCred = <\/span><span style=\"background: white;color: blue\">new <\/span><span style=\"background: white;color: #2b91af\">BasicAuthCredential<\/span><span style=\"background: white;color: black\">(netCred);\n            <\/span><span style=\"background: white;color: #2b91af\">TfsClientCredentials <\/span><span style=\"background: white;color: black\">tfsCred = <\/span><span style=\"background: white;color: blue\">new <\/span><span style=\"background: white;color: #2b91af\">TfsClientCredentials<\/span><span style=\"background: white;color: black\">(basicCred);\n            tfsCred.AllowInteractive = <\/span><span style=\"background: white;color: blue\">false<\/span><span style=\"background: white;color: black\">;\n\n            <\/span><span style=\"background: white;color: #2b91af\">TfsTeamProjectCollection <\/span><span style=\"background: white;color: black\">tpc = <\/span><span style=\"background: white;color: blue\">new <\/span><span style=\"background: white;color: #2b91af\">TfsTeamProjectCollection<\/span><span style=\"background: white;color: black\">(\n                <\/span><span style=\"background: white;color: blue\">new <\/span><span style=\"background: white;color: #2b91af\">Uri<\/span><span style=\"background: white;color: black\">(<\/span><span style=\"background: white;color: #a31515\">&quot;https:\/\/YourAcct.visualstudio.com\/DefaultCollection&quot;<\/span><span style=\"background: white;color: black\">),\n                tfsCred);\n\n            tpc.Authenticate();\n\n            <\/span><span style=\"background: white;color: #2b91af\">Console<\/span><span style=\"background: white;color: black\">.WriteLine(tpc.InstanceId);\n        }\n    }\n}<\/span><\/pre>\n<p>I\u2019ve added two using statements, one for <span style=\"font-family: courier new\">System.Net<\/span> to pull in <span style=\"font-family: courier new\">NetworkCredential<\/span> and one for <span style=\"font-family: courier new\">Microsoft.TeamFoundation.Client<\/span> for the TFS classes we\u2019ll need.<\/p>\n<p>The first thing we construct is a standard <span style=\"font-family: courier new\">NetworkCredential<\/span> object with the username (the email address that you use for your Microsoft Account) and the password that you created for alternate credentials. On the <span style=\"font-family: courier new\">TfsClientCredentials<\/span> object, we set <span style=\"font-family: courier new\">AllowInteractive<\/span> to false to prevent a prompt dialog being shown if the credentials are invalid.<\/p>\n<p>In constructing the <span style=\"font-family: courier new\">TfsTeamProjectCollection<\/span>, we must specify the URL to the collection and the credentials. Note that all connections to accounts in TF Service require https. Currently, there is only one collection per account in TF Service, so it is always DefaultCollection.<\/p>\n<p>Finally, we call <span style=\"font-family: courier new\">Authenticate()<\/span> to verify that we have supplied the correct credentials and test that it is working by printing the unique <span style=\"font-family: courier new\">InstanceId<\/span> of the collection.<\/p>\n<p>Now the rest of the TFS client object model is available for use with the TF Service from applications that cannot prompt for credentials.<\/p>\n<p>Enjoy!<\/p>\n<p><em>Follow me on Twitter at <\/em><a href=\"https:\/\/twitter.com\/tfsbuck\"><em>https:\/\/twitter.com\/tfsbuck<\/em><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Normally when you connect to the Team Foundation Service you are presented with a web page to log in with your Microsoft Account (aka LiveID). When you log in you can choose to have it remember you and you won\u2019t have to enter your Microsoft Account credentials again (unless you don\u2019t log in again for [&hellip;]<\/p>\n","protected":false},"author":94,"featured_media":10268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[8,15,17],"class_list":["post-233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-team-foundation","tag-tfs-api","tag-tfservice"],"acf":[],"blog_post_summary":"<p>Normally when you connect to the Team Foundation Service you are presented with a web page to log in with your Microsoft Account (aka LiveID). When you log in you can choose to have it remember you and you won\u2019t have to enter your Microsoft Account credentials again (unless you don\u2019t log in again for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/users\/94"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media\/10268"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}