2014-10-29 76 views
1

我們目前在Sitecore中實現WCF服務來執行某些任務。不過,我們希望保護和驗證這些交互,以保持Sitecore安全模型不變。WCF服務認證+ Sitecore

我們採用以下配置的身份驗證(唯一相關的配置和匿名):

<service name="Services.MailService" behaviorConfiguration="serviceBehavior"> 
    <endpoint address="" binding="wsHttpBinding" contract="Interfaces.IMailService"/> 
</service> 
<behavior name="serviceBehavior"> 
    <serviceCredentials> 
     <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Services.Authentication.CustomServiceAuthentication, MyLibrary" /> 
    </serviceCredentials> 
</behavior> 
<wsHttpBinding> 
    <binding> 
     <security mode="TransportWithMessageCredential"> 
      <message clientCredentialType="UserName" /> 
      <transport clientCredentialType="None"> 
      </transport> 
     </security> 
    </binding> 
</wsHttpBinding> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/> 

自定義驗證從UserNamePasswordValidator繼承並記錄用戶在使用標準Sitecore.Security.Authentication.AuthenticationManager.Login( ) 方法。在這個確切的時刻,用戶確實登錄並顯示爲Sitecore.Context.User。但是當到達WCF方法本身時,這個身份驗證就不存在了。 (導致訪問來自Sitecore的異常,因爲匿名用戶沒有添加項目權限)

經過幾次測試並研究了交互之後,我注意到問題在於WCF使用多個消息,因此正在使用多個HttpContext。請求之間沒有保留cookie和登錄。更深入地看,我注意到System.ServiceModel.ServiceSecurityContext.Current確實保留了安全登錄,但它只有在進入WCF方法後才顯示出來(即不能在Sitecore httpBeginRequest管道中使用它來識別並在UserResolver登錄用戶)

如何確保asp.net和wcf在整個調用過程中都得到了正確的身份驗證?

+0

我猜因爲認證是騎在消息和不是運輸水平,它沒有被拿起... – IvanL 2014-10-30 11:16:02

+0

你有沒有找到解決辦法呢?同樣的問題在這裏 – geedubb 2015-08-13 07:03:18

+1

@geedubb我添加了我們用來解決問題的答案 – IvanL 2015-08-13 09:45:35

回答

1

在我們結束了通過在服務的構造以下,因爲我們的InstanceContextMode設置爲PerCall解決這個結尾:

// Handle login for Sitecore to sync with the WCF security context 
if (ServiceSecurityContext.Current != null) 
{ 
    AuthenticationManager.Login(
    string.Format("{0}\\{1}", "yoursitecoredomain", ServiceSecurityContext.Current.PrimaryIdentity.Name)); 
}