Tuesday, April 5, 2011

Excel Service REST: authentication in C#

The service reference of excel service in SharePoint Server 2010 is

http://sp2010/_vti_bin/ExcelService.asmx

I add above service reference in a console application. The code in the application is shown below:

ExcelServiceSoapClient client = new ExcelServiceSoapClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel =           System.Security.Principal.TokenImpersonationLevel.Impersonation;
String url = "http://sp2010/excelFiles/Book1.xlsx";
Status[] status;
String session = client.OpenWorkbook(url, "en-US", "en-US", out status);
When I runs the above code, method OpenWorkbook() throws an MessageSecurityException as below:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate, NTLM'.
The message indicates the problem is related to the authentication method. Go to app.config and change the security settings.

<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="Ntlm"  realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
 If you do not want to use current window authentication, you can specify a username account.

client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(username, password, domain);
                client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;    

No comments:

Post a Comment