Share via


Authenticating Team Foundation Server Users

Use the ICredentialsProvider interface to authenticate a user—someone other than the current user—by using non-default credentials. The UICredentialsProvider class implements this interface.

Authentication

The following code example shows you how to use non-default credentials.

Handling Credentials from a Console Application

The following code example parses the /login option or sets default credentials, among other things.

Command command = CreateCommand(args);
ICredentialsProvider provider = new UICredentialsProvider();
TeamFoundationServer m_tfs = TeamFoundationServerFactory.GetServer(m_server, provider);
while (true)
   {
    try
       {
// A Command requests by URI a TeamFoundationServer object
// from a CommandLine object after it determines the URI. A
// CommandLine object has a method that you can use to get
// cached credentials from the credentials provider based
// on a user name and URL.
        command.Run();
        break;
       }
     catch (UnauthorizedException)
        {
// Now prompt the user. If it succeeds, try the command again.
// Otherwise, Authenticate() throws an Unauthorized exception
// and breaks out of this loop.
        m_tfs.Authenticate();
        }
    }

Handling Credentials on a Server Application

The following code example shows you how to use default credentials, such as ASNET user, and how to retrieve information from the registry and DPAPI cache.

TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(m_server);
        try
        {
// Determine whether you can start.
          tfs.Authenticate();
// Make tfs calls
// If any subsequent call fails and throws the unauthorized
// exception, the whole thing ends. This could occur when,
// for example, the current user's account password expires.
        }
        catch (UnauthorizedException)
        {
// Record the problem by writing an event to event log,
// sending e-mail, updating a status Web page, or other method.
        }

Handling Credentials for a Windows-Based Application

The following code example shows you how to handle credentials for a Windows-based application.

// Start the application.

// At some point, make the first contact with the server 
// by authenticating a logged-on user or whatever is specified
// in the Visual Studio Options dialog box.
ICredentialsProvider provider = new UICredentialsProvider();
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(m_server, provider);
// Make sure you can continue.
        try
        {
// Try to authenticate. If unsuccessful, use tfsUICredProvider to
// request credentials.
            tfs.Authenticate();
        }
        catch (UnauthorizedException)
        {
// User cannot be authenticated. Shut down the application.
        }
// Try other things through the user interface. At any time,
// an unauthorized exception might occur because of password
// expiration, changing users by dropping and reconnecting
// to the VPN, and so on. The application developer must
// decide how to handle exceptions and call tfs.Authenticate()
// to catch the exception.

See Also

Concepts

Essentials

Connecting to a Team Foundation Server

Working with Team Foundation Server Properties

Working with Team Foundation Server Services