[英]Impersionation like in ASP.NET for WCF Service
我在同一個web.config中有一個帶有ASP.NET站點和WCF服務的WebService。 到目前為止,我可以通過設置WCF服務中的ASP.NET命令
<system.web>
<compilation targetFramework="4.0" debug="false"/>
<!-- switch custom errors of-->
<identity impersonate="true"/>
<customErrors mode="Off"/>
</system.web>
但是,現在(出於其他原因-> ASP.NET部分的Cookieless Session狀態),我必須設置
aspNetCompatibilityEnabled="true"
選項為false。 這樣,我就失去了WCF服務的ASP.NET功能。 我的WCF服務之一需要在服務器上進行IO操作的命令...我想知道如何通過直接在WCF服務配置上定義它來獲得與以前相同的命令。
我嘗試過(失敗)的是設置
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
WCF服務中方法的實現,然后指定
<endpoint address="" binding="wsHttpBinding" contract="IService">
<identity>
<servicePrincipalName value="HOST/YourMachineName" />
<dns value="" />
</identity>
</endpoint>
如http://msdn.microsoft.com/en-us/library/ff650591.aspx中所述,在web.config中(顯然具有我的服務的正確值)。
但是,此后將無法再調用WCF服務...它告訴我WsHttpBinding不提供合同的身份。
我缺少重要的東西嗎?
編輯:錯誤消息的翻譯:
:合同操作'{0}'需要Windows身份才能自動模擬。 合同('{3}','{4}')的綁定('{1}','{2}')不能提供代表調用者的Windows身份。
(原始錯誤消息是德語...)
嘗試添加類似的東西
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="DelegationBehaviour">
<clientCredentials>
<windows allowNtlm="false" allowedImpersonationLevel="Delegation"></windows>
</clientCredentials>
<dataContractSerializer maxItemsInObjectGraph="4194304"></dataContractSerializer>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_SampleWebService" >
<readerQuotas maxArrayLength="16384" maxBytesPerRead="4096" maxDepth="32" maxNameTableCharCount="16384" maxStringContentLength="8192"></readerQuotas>
<security mode="TransportCredentialOnly">
<message algorithmSuite="Default" clientCredentialType="UserName"></message>
<transport clientCredentialType="Windows" proxyCredentialType="None" realm=""></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://server/WebServices/Service/Service.svc" behaviorConfiguration="DelegationBehaviour" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SampleWebService" contract="SampleWS" name="BasicHttpBinding_SampleEndpoint"></endpoint>
</client>
</system.serviceModel>
這是服務器端代碼
<system.serviceModel>
<services>
<service behaviorConfiguration="CustomBehavior" name="CustomWebService">
<endpoint address="" behaviorConfiguration="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding_Service" contract="WebService"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding_Service" maxReceivedMessageSize="4194304" receiveTimeout="00:30:00">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
<message clientCredentialType="UserName"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="CustomBehavior">
<dataContractSerializer maxItemsInObjectGraph="4194304" ignoreExtensionDataObject="True"/>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
<serviceAuthorization impersonateCallerForAllOperations="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
以及在我們的WebMethods中擁有這些
<WebMethod(), OperationContract(), OperationBehavior(Impersonation:=ImpersonationOption.Required)> _
為我們工作
好吧,最后,我只使用Windows身份驗證進行了綁定:
<security mode="TransportWithMessageCredential">
<message negotiateServiceCredential="false" clientCredentialType="Windows" algorithmSuite="Default"/>
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
</security>
並在客戶端中傳遞了特定的Windows用戶/密碼組合:
channelFactory.Credentials.Windows.ClientCredential = new NetworkCredential(@"", "", "");
channelFactory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
另外,我必須在Web服務的代碼中專門使用新模擬的用戶:
using (var imp = ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
{
// do IO here
}
好吧,實際(基礎)問題仍然存在:
如何正確模擬ASP.NET功能...
目前,我對解決方案還可以,但是我有一種感覺,就是我錯過了有關ASP.NET模擬的重要要點。
非常感謝Iain,雖然這不是正確的答案,但至少使我走上了正確的道路!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.