Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
You must set credentials for your users before they can call Servicios web de Excel by using your custom application. You must explicitly set credentials even if you intend to use the default credentials. Servicios web de Excel uses the authentication schemes that Microsoft SharePoint Foundation supports. Para obtener más información sobre los esquemas de autenticación de SharePoint Foundation, vea la documentación de SharePoint Foundation en este SDK y Notificaciones entrantes: Iniciar sesión en SharePoint.
En los siguientes ejemplos se muestra cómo establecer las credenciales.
Para usar las credenciales del usuario actual
El siguiente código usa las credenciales de inicio de sesión del usuario actual para realizar una solicitud al servicio web.
//Instantiate the Web service.
ExcelService xlService = new ExcelService();
//Set credentials for requests.
xlService.Credentials = System.Net.CredentialCache.DefaultCredentials;
'Instantiate the Web service.
Dim xlService As New ExcelService()
'Set credentials for requests.
xlService.Credentials = System.Net.CredentialCache.DefaultCredentials
To use various sets of credentials
The following code uses the current user's logon credentials to make a request to the Web service.
Sample code provided by: Saif Ullah Baig, Microsoft Corporation.
protected string farmURL, docLibPath, workbookPath, uiCulture, dataCulture, localTempFolder, authenticationType;
protected Cookie authCookie;
protected API.ExcelService api;
protected Constants.XLS_VER version;
public VariousAuthScheme(Constants.XLS_VER ver, string farmurl, string docLib, string fileName,
string uic, string datac,
string userName, string password, string domain,
string localTemp, string authType)
{
api = new API.ExcelService();
farmURL = farmurl;
if (!farmURL.EndsWith("/"))
{
farmURL += "/";
}
api.Url = farmURL + "_vti_bin/ExcelService.asmx";
version = ver;
if (!docLib.EndsWith("/"))
{
docLib += "/";
}
workbookPath = farmURL + docLib + fileName;
docLibPath = farmURL + docLib;
uiCulture = uic;
dataCulture = datac;
localTempFolder = localTemp;
authenticationType = authType;
switch (authType)
{
case "Windows-Classic":
authenticationType = "Windows-Classic";
AuthenticateWindowsClassic(domain, userName, password);
break;
case "Windows-Claims":
authenticationType = "Windows-Claims";
AuthenticateWindowsClaims();
break;
case "FBA-Claims":
authenticationType = "FBA-Claims";
if (!AuthenticateFBAClaims(userName, password))
throw new Exception("FBA-Claims authentication failed");
break;
case "Anonymous":
authenticationType = "Anonymous";
break;
default:
throw new Exception ("Undefined authentication type specified: " + authType);
break;
}
}
protected void AuthenticateWindowsClassic(string domain, string userName, string password)
{
if (userName != null && userName.Length > 0)
{
api.Credentials = new System.Net.NetworkCredential(userName, password, domain);
}
else
{
api.Credentials = System.Net.CredentialCache.DefaultCredentials;
}
// Verify set credentials.
System.Net.NetworkCredential cred = (System.Net.NetworkCredential) api.Credentials;
Console.WriteLine(@"Credentials set to: {0}\\{1}", cred.Domain, cred.UserName);
}
protected void AuthenticateWindowsClaims()
{
throw new Exception ("Windows-Claims Authentication method not implemented");
}
protected bool AuthenticateFBAClaims(string userName, string password)
{
FBA.Authentication spAuthentication = new FBA.Authentication();
spAuthentication.Url = farmURL + "_vti_bin/Authentication.asmx";
spAuthentication.CookieContainer = new CookieContainer();
FBA.LoginResult loginResult = spAuthentication.Login(userName, password);
authCookie = new Cookie();
// Determines if login is successful.
if (loginResult.ErrorCode == FBA.LoginErrorCode.NoError)
{
// Get the cookie collection from the authenticating Web service.
CookieCollection cookies = spAuthentication.CookieContainer.GetCookies(new Uri(spAuthentication.Url));
// Get the specific cookie that contains the security token.
authCookie = cookies[loginResult.CookieName];
// Initialize the cookie container of Excel Web Services.
api.CookieContainer = new CookieContainer();
api.CookieContainer.Add(authCookie);
return true;
}
else
{
return false;
}
Para usar un conjunto diferente de credenciales
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();
public void VerifyCredentials()
{
//Check whether the default credentials
//should be used instead.
if (DefaultCredentialsCheckBox.Checked)
{
xlService.Credentials =
System.Net.CredentialCache.DefaultCredentials;
}
else
{
//Check whether user-defined credentials
//should be used instead.
System.Net.NetworkCredential userDefined = new
System.Net.NetworkCredential(
LoginNameTextBox.Text,
LoginPWDTextBox.Text,
LoginDomainTextBox.Text);
xlService.Credentials = userDefined;
}
}
'Instantiate the Web service.
Private xlService As New ExcelService()
Public Sub VerifyCredentials()
'Check whether the default credentials
'should be used instead.
If DefaultCredentialsCheckBox.Checked Then
xlService.Credentials = System.Net.CredentialCache.DefaultCredentials
Else
'Check whether user-defined credentials
'should be used instead.
Dim userDefined As New System.Net.NetworkCredential(LoginNameTextBox.Text, LoginPWDTextBox.Text, LoginDomainTextBox.Text)
xlService.Credentials = userDefined
End If
End Sub
In this example, LoginNameTextBox, LoginPWDTextBox, and LoginDomainTextBox are the Name property values of the logon text boxes.
For more information about how to use the CredentialCache class and the NetworkCredential class, and how to use them securely, see the Microsoft Visual Studio documentation, or NetworkCredential Class.
Vea también
Conceptos
Obtener acceso a la API de SOAP
Otros recursos
Paso 1: Crear el proyecto cliente del servicio web
Paso 2: Agregar una referencia web
Paso 3: acceso al servicio web
Paso 4: Crear y probar la aplicación
Tutorial: Desarrollar una aplicación personalizada mediante Excel Web Services