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.

Comments (9)

  1. Ravi Terala says:

    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.

  2. Jeff Beehler says Team Foundation Server Ships!  And so does John Lawrence, complete with a picture of…

  3. hippietim says:

    I think you mean EnsureAuthenticated

    :P

  4. Ravi Terala says:

    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.

  5. buckh says:

    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.

  6. Buck Hodges says:

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

  7. Buck Hodges says:

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

  8. Shai Raiten says:

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

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

Skip to main content