How to: Set Credentials

You must set credentials for your users before they can call Excel Web Services using your custom application. You must explicitly set credentials even if you intend to use the default credentials.

Excel Web Services uses the authentication schemes that Windows SharePoint Services 3.0 supports. For more information about Windows SharePoint Services 3.0 authentication schemes, see the Windows SharePoint Services 3.0 SDK.

The following examples show how to set credentials.

To use the current user's credentials

  • The following code uses the current user's logon credentials to make a request to the Web service.

    //Instantiate the Web service
        ExcelService xlService = new ExcelService();
    //Set credentials for requests
        xlService.Credentials = System.Net.CredentialCache.DefaultCredentials;
    

To use a different set of credentials

  1. In the following example, users can choose to use the current user's logon credentials or another set of credentials. The following code checks which credentials should be used.

    //Instantiate the Web service.
    ExcelService xlService = new ExcelService();
    
    public void VerifyCredentials()
       {
        //Check if the default credentials
        //should be used instead.  
           if (DefaultCredentialsCheckBox.Checked)
     {
         xlService.Credentials =     
            System.Net.CredentialCache.DefaultCredentials;
      }
      else
      {
          //Check if user-defined credentials
             //should be used instead.
          System.Net.NetworkCredential userDefined = new 
             System.Net.NetworkCredential(
                LoginNameTextBox.Text,
    LoginPWDTextBox.Text,
    LoginDomainTextBox.Text);
    
             xlService.Credentials = userDefined;          
          }
    }
    
  2. In this example, LoginNameTextBox, LoginPWDTextBox, and LoginDomainTextBox are the logon text boxes' Name property values.

Important   For more information about how to use the CredentialCache and NetworkCredential classes and how to use them securely, see the Microsoft Visual Studio 2005 documentation, or NetworkCredential Class (https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetnetworkcredentialclasstopic.asp) in the .NET Framework Class Library documentation.

See Also

Tasks

Step 1: Creating the Web Service Client Project
Step 2: Adding a Web Reference
Step 3: Accessing the Web Service
Step 4: Building and Testing the Application
Walkthrough: Developing a Custom Application Using Excel Web Services
How to: Trust Workbook Locations Using Script

Concepts

Accessing the SOAP API