Welcome to MSDN Blogs Sign in | Join | Help

How to get the login dialog when using the Team Foundation Server API

Someone asked how to get the login dialog if the user fails to authenticate.  The code to do that is shown below.  I copied the call to the constructor from the basic API example, and I've added code to create and pass a UICredentialsProvider object to the TeamFoundationServer constructor.

Also, I'm now calling EnsureAuthenticated().  The purpose of that call is to make sure that the user is able to authenticate with the server.  If not, the TeamFoundationServer object will use the UICredentialsProvider to display a login dialog.  If the user authenticates successfully after entering a user name and password, the program will continue execution.  If the user clicks the Cancel button in the login dialog, perhaps after failing to enter a valid user name and password, the EnsureAuthenticated() method will throw TeamFoundationServerUnauthorizedException.

If you look at the TeamFoundationServer class, you will notice that there are two authentication methods to call: Authenticate() and EnsureAuthenticated().  The difference is that Authenticate() will always call the server, and EnsureAuthenticated() will only call the server if the user has not already authenticated, resulting in no performance penalty for calling it multiple times.

Only Authenticate() and EnsureAuthenticated() will display the login dialog.  Any other method calls that fail due to the user not authenticating will result in TeamFoundationServerUnauthorizedException being thrown.

    // Get a reference to our Team Foundation Server. 
    TeamFoundationServer tfs = new TeamFoundationServer(tfsName, new UICredentialsProvider());

    // Authenticate with the server if we haven't already (in this example, we haven't).
    // If authentication fails, the user will see the login dialog, as a result of
    // UICredentialsProvider() being specified above.

    tfs.EnsureAuthenticated();

[Update 3/20/06]  Fixed typo.  Thanks, Tim.

Published Friday, March 17, 2006 8:40 AM by buckh

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: How to get the login dialog when using the Team Foundation Server API

I recommend using TeamFoundationServerFactory for most purposes. You only need the constructor version if you need two TFS OM objects to the same server using two different credentials.
Friday, March 17, 2006 11:16 AM by Ravi Terala

# VSTS Links - 03/20/2006

Jeff Beehler says Team Foundation Server Ships!  And so does John Lawrence, complete with a picture of...
Monday, March 20, 2006 9:14 AM by Team System News

# Typo

I think you mean EnsureAuthenticated

:P
Monday, March 20, 2006 2:18 PM by hippietim

# re: How to get the login dialog when using the Team Foundation Server API

tfs = TeamFoundationServerFactory.GetServer("tfsurl
, new UICredentialsProvder());
tfs.EnsureAuthenticated();

will give you the login dialog _if_ needed. It will grab the stored credentials from the stored user names and passwords control panel applet if it contains credentials for the TFS server.
Monday, March 20, 2006 7:19 PM by Ravi Terala

# re: How to get the login dialog when using the Team Foundation Server API

Ravi, that's not correct.  The TeamFoundationServer object does not write to or read from the stored user names and passwords.  There was a time when it used it, but it was buggy and was removed.
Monday, March 20, 2006 9:01 PM by buckh

# TFS API: How to choose between TeamFoundationServerFactory.GetServer() and the TeamFoundationServer constructor

Occasionally the question of whether to use the TeamFoundationServerFactory.GetServer() method or the...
Thursday, September 14, 2006 10:36 PM by Buck Hodges

# TFS API: How to choose between TeamFoundationServerFactory.GetServer() and the TeamFoundationServer constructor

Occasionally the question of whether to use the TeamFoundationServerFactory . GetServer() method or the

Thursday, August 30, 2007 11:06 PM by Buck Hodges

# How to get the login dialog when using the Team Foundation Server API

How to get the login dialog when using the Team Foundation Server API Published On: Buck Hodges Blog

Sunday, September 21, 2008 1:41 PM by Shai Raiten

# Login dialog when using the Team Foundation Server API

nwc = new NetworkCredential(tfsuid, TFSPass, TFSDomain); tfs = new TeamFoundationServer(TFSServerUrl

Friday, February 06, 2009 4:22 PM by Suresh Behera

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker