[英]Impersonate User in SharePoint 2010 web part to call WCF service
[英]How to properly call an external WCF Service from SharePoint 2010?
我想從 SharePoint 中調用外部 WCF 服務。 使用正常的 WCF 調用會立即導致SocketException
(現有連接被遠程主機強制關閉),可以使用SPSecurity.RunWithElevatePrivileges
規避。
[SocketException (0x2746): 現有連接被遠程主機強行關閉]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +85
System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] 緩沖區,Int32 偏移量,Int32 大小,TimeSpan 超時,Boolean 關閉)+204[CommunicationException:套接字連接被中止。 這可能是由於處理您的消息時出錯或遠程主機超出接收超時,或者是潛在的網絡資源問題造成的。 本地套接字超時為 '00:00:58.7210000'。]
System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] 緩沖區,Int32 偏移量,Int32 大小,TimeSpan 超時,Boolean 關閉)+15307563
System.ServiceModel.Channels.SocketConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +90
System.ServiceModel.Channels.DelegatingConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +34
System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout) +34
System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count) +88
System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) +58
System.Net.Security.NegotiateStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +62
System.Net.Security.NegotiateStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +54
System.Net.Security.NegotiateStream.ProcessRead(Byte[] 緩沖區,Int32 偏移量,Int32 計數,AsyncProtocolRequest asyncRequest) +402[IOException:讀取操作失敗,請參閱內部異常。]
System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +704
System.Net.Security.NegotiateStream.Read(Byte[] buffer, Int32 offset, Int32 count) +154
System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +87[CommunicationException:套接字連接被中止。 這可能是由於處理您的消息時出錯或遠程主機超出接收超時,或者是潛在的網絡資源問題造成的。 本地套接字超時為 '00:00:58.7210000'。]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10257978
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 類型) +539
MyApp.FunctionThatCallsService()
這讓我想到: SharePoint 做到了,而且他們做得很好,他們在14\WebClients
中有單獨的服務配置。
我反映並克隆了很多(ChannelFactory 上的擴展方法在內部到處都在使用 - SPChannelFactoryOperations ),但我想知道:SharePoint 用來調用它自己的 WCF 服務的“正確”API 是否暴露給外部代碼,以便我可以從自定義 SharePoint 代碼調用我的非 SharePoint WCF 服務?
(這是部署到 GAC 的場解決方案,因此 CAS 和沙盒都不適用)
最終保留了SPSecurity.RunWithElevatedPrivileges
部分。 Microsoft 的SPChannelFactoryOperations
在某些時候遇到 NullReferenceException,很可能是由於即使不使用它也需要在服務中設置聲明( SPServiceAuthenticationMode
只有SPServiceAuthenticationMode.Claims
作為成員CreateChannelActingAsLoggedOnUser()
立即拋出 ArgumentException 的事實是一個信號SPChannelFactoryOperations
不打算由非 Microsoft 代碼使用。
我建議嘗試忽略證書。
http://www.fryan0911.com/2006/07/vbnet-how-to-accept-web-service-ssl.html
也許您需要代理? 您是否遇到某種類型的端口阻塞問題?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.