简体   繁体   English

如何使用与Sharepoint相同的凭据调用Web服务?

[英]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? 是否可以使用与用于访问Sharepoint服务器的凭据相同的凭据,从已从Sharepoint服务器下载的Excel工作表中进行Web服务调用?

We're currently developing an Excel solution, which does webservice request from within the Excel sheet. 我们目前正在开发一个Excel解决方案,该解决方案可以在Excel工作表中执行Web服务请求。 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. 这可以正常工作,但是用户必须至少登录两次:一种用于从Sharepoint下载/打开Excel工作表,另一种能够使用正确的凭据执行Web服务。

The Sharepoint server and the client machine are not in the same Active Directory domain. Sharepoint服务器和客户端计算机不在同一Active Directory域中。 So "System.Security.Principal.WindowsIdentity.GetCurrent()" is not an option, since this will return a user that doesn't exist on the server. 因此,“ System.Security.Principal.WindowsIdentity.GetCurrent()”不是一个选项,因为这将返回服务器上不存在的用户。

You can use VSTO (Visual Studio Tools For Office) to create an Excel plugin. 您可以使用VSTO(用于Office的Visual Studio工具)创建Excel插件。 The plugin gets loaded every time you open the Excel and can contain buttons etc. 每次您打开Excel时都会加载该插件,并且可以包含按钮等。

To use the system account of Sharepoint you must use RunWithElevatedPrivileges. 若要使用Sharepoint的系统帐户,必须使用RunWithElevatedPrivileges。 This has some security implications! 这有一些安全隐患! http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx http://msdn.microsoft.com/zh-CN/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

Normally the webservices take the logged-in user credentials into account. 通常,Web服务会考虑已登录的用户凭据。 if not you can always manually create them by using NetworkCredential class. 如果不是,则始终可以使用NetworkCredential类手动创建它们。

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 //在此处访问您的Web服务方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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