简体   繁体   中英

How to call webservice using same credentials as Sharepoint?

Is it possible to do a webservice call from within an Excel sheet that has been downloaded from a sharepoint server, using the same credentials as the ones that are used for accessing the Sharepoint server?

We're currently developing an Excel solution, which does webservice request from within the Excel sheet. This works fine, but the user has to log in at least twice : one for downloading/opening the Excel sheet from Sharepoint, and one to be able to execute the webservice using the right credentials.

The Sharepoint server and the client machine are not in the same Active Directory domain. So "System.Security.Principal.WindowsIdentity.GetCurrent()" is not an option, since this will return a user that doesn't exist on the server.

You can use VSTO (Visual Studio Tools For Office) to create an Excel plugin. The plugin gets loaded every time you open the Excel and can contain buttons etc.

To use the system account of Sharepoint you must use RunWithElevatedPrivileges. This has some security implications! http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

Normally the webservices take the logged-in user credentials into account. if not you can always manually create them by using NetworkCredential class.

System.Net.CredentialCache myCredentials = new System.Net.CredentialCache();
NetworkCredential netCred = new NetworkCredential("UserName", "Password");
myCredentials.Add(new Uri(myService.Url), "Basic", netCred);
myService.Credentials = myCredentials;

// Acces your webservice methods here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM