{"id":4103,"date":"2006-03-17T08:40:00","date_gmt":"2006-03-17T08:40:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/buckh\/2006\/03\/17\/how-to-get-the-login-dialog-when-using-the-team-foundation-server-api\/"},"modified":"2006-03-17T08:40:00","modified_gmt":"2006-03-17T08:40:00","slug":"how-to-get-the-login-dialog-when-using-the-team-foundation-server-api","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/buckh\/how-to-get-the-login-dialog-when-using-the-team-foundation-server-api\/","title":{"rendered":"How to get the login dialog when using the Team Foundation Server API"},"content":{"rendered":"<p>Someone asked how to get the login dialog if the user fails to authenticate.&nbsp; The code to do that is shown below.&nbsp; I copied the call to the constructor from the <a href=\"http:\/\/blogs.msdn.com\/buckh\/archive\/2006\/03\/15\/552288.aspx\">basic API example<\/a>, and I&#8217;ve added code to create and pass a <font face=\"Courier New\">UICredentialsProvider<\/font> object to the <font face=\"Courier New\">TeamFoundationServer<\/font> constructor.<\/p>\n<p>Also, I&#8217;m now calling <font face=\"Courier New\">EnsureAuthenticated()<\/font>.&nbsp; The purpose of that call is to make sure that the user is able to authenticate with the server.&nbsp; If not, the <font face=\"Courier New\">TeamFoundationServer<\/font> object will use the <font face=\"Courier New\">UICredentialsProvider<\/font> to display a login dialog.&nbsp; If the user authenticates successfully after entering a user name and password, the program will continue execution.&nbsp; If the user clicks the Cancel button in the login dialog, perhaps after failing to enter a valid user name and password, the <font face=\"Courier New\">EnsureAuthenticated()<\/font> method will throw <font face=\"Courier New\">TeamFoundationServerUnauthorizedException<\/font>.<\/p>\n<p>If you look at the <font face=\"Courier New\">TeamFoundationServer<\/font> class, you will notice that there are two authentication methods to call: <font face=\"Courier New\">Authenticate()<\/font> and <font face=\"Courier New\">EnsureAuthenticated()<\/font>.&nbsp; The difference is that <font face=\"Courier New\">Authenticate()<\/font> will always call the server, and <font face=\"Courier New\">EnsureAuthenticated()<\/font> will only call the server if the user has not already authenticated, resulting in no performance penalty for calling it multiple times.<\/p>\n<p>Only <font face=\"Courier New\">Authenticate()<\/font> and <font face=\"Courier New\">EnsureAuthenticated()<\/font> will display the login dialog.&nbsp; Any other method calls that fail due to the user not authenticating will result in <font face=\"Courier New\">TeamFoundationServerUnauthorizedException<\/font> being thrown.<\/p>\n<p><font face=\"Courier New\">&nbsp;&nbsp;&nbsp;&nbsp;<span>\/\/&nbsp;Get&nbsp;a&nbsp;reference&nbsp;to&nbsp;our&nbsp;Team&nbsp;Foundation&nbsp;Server.<\/span>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;TeamFoundationServer&nbsp;tfs&nbsp;=&nbsp;<span>new<\/span>&nbsp;TeamFoundationServer(tfsName, new UICredentialsProvider());<\/p>\n<p>&nbsp;&nbsp;&nbsp; <span>\/\/&nbsp;Authenticate with the server if we haven&#8217;t already (in this example, we haven&#8217;t).<br>&nbsp;&nbsp;&nbsp; \/\/ If authentication fails, the&nbsp;user will see the login dialog, as a result of<br>&nbsp;&nbsp;&nbsp; \/\/ UICredentialsProvider() being specified above.<\/span><\/font><font face=\"Courier New\"><br>&nbsp;&nbsp;&nbsp;&nbsp;tfs.EnsureAuthenticated();<\/font><\/p>\n<p>[Update 3\/20\/06]&nbsp; Fixed typo.&nbsp; Thanks, Tim.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Someone asked how to get the login dialog if the user fails to authenticate.&nbsp; The code to do that is shown below.&nbsp; I copied the call to the constructor from the basic API example, and I&#8217;ve added code to create and pass a UICredentialsProvider object to the TeamFoundationServer constructor. Also, I&#8217;m now calling EnsureAuthenticated().&nbsp; The [&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":[6,8,15],"class_list":["post-4103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-source-control","tag-team-foundation","tag-tfs-api"],"acf":[],"blog_post_summary":"<p>Someone asked how to get the login dialog if the user fails to authenticate.&nbsp; The code to do that is shown below.&nbsp; I copied the call to the constructor from the basic API example, and I&#8217;ve added code to create and pass a UICredentialsProvider object to the TeamFoundationServer constructor. Also, I&#8217;m now calling EnsureAuthenticated().&nbsp; The [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/4103","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=4103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/4103\/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=4103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/categories?post=4103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/tags?post=4103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}