2013-02-13 18 views
1

我在主題行中提到的設置上苦苦掙扎,並且想知道是否有人可以幫助我。在WCF中處理REST SWT令牌WIF管道無法正常工作

本質上,我所擁有的是一個WCF服務,並且我想實現用戶可以使用自定義登錄頁面(使用JavaScript和ACS提供的所需信息使用JavaScript)對ACS進行身份驗證。

這樣做後,用戶應該使用提供的SWT令牌重定向到WCF服務。我使用SimpleWebTokenHandler作爲SWT令牌處理的基礎,但我不確定它在這方面發揮了什麼作用。

這裏是我跑

<configuration> 
    <configSections> 
     <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> 
     <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral" /> 
    </configSections> 
    ... 
    <system.serviceModel> 
     <diagnostics> 
     </diagnostics> 
     <services> 
      <service name="WcfWifSwtAcs.Service1"> 
       <endpoint address="xmlService" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="restPoxBehaviour" name="xmlServiceEndpoint" contract="WcfWifSwtAcs.IService1" /> 
      </service> 
     </services> 
     <behaviors> 
      <endpointBehaviors> 
       <behavior name="restPoxBehaviour"> 
        <webHttp helpEnabled="true" /> 
       </behavior> 
      </endpointBehaviors> 
      <serviceBehaviors> 
       <behavior> 
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="true" /> 
        <serviceCredentials useIdentityConfiguration="true"> 
         ... 
        </serviceCredentials> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <protocolMapping> 
      <add scheme="http" binding="ws2007FederationHttpBinding" /> 
     </protocolMapping> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
     <bindings> 
      <ws2007FederationHttpBinding> 
       <binding name=""> 
        <security mode="Message"> 
         <message 
          issuedTokenType="http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0"> 
          <issuerMetadata address="https://xxxx.accesscontrol.windows.net/v2/wstrust/13/certificate/mex" /> 
         </message> 
        </security> 
       </binding> 
      </ws2007FederationHttpBinding> 
     </bindings> 
    </system.serviceModel> 
    <system.webServer> 
    ... 
    </system.webServer> 
    <system.identityModel> 
     <identityConfiguration> 
      <audienceUris> 
       <add value="http://localhost:56782/Service1.svc" /> 
      </audienceUris> 
      <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
       <trustedIssuers> 
        <add thumbprint="XXX" name="xxx.accesscontrol.windows.net" /> 
       </trustedIssuers> 
      </issuerNameRegistry> 
     <issuerTokenResolver type="SimpleWebToken.CustomIssuerTokenResolver, WcfWifSwtAcs" /> 
      <securityTokenHandlers> 
       <clear/> 
       <add type="SimpleWebToken.SimpleWebTokenHandler, WcfWifSwtAcs"/> 
      </securityTokenHandlers> 
     </identityConfiguration> 
    </system.identityModel> 
</configuration> 

現在我可以看到的Web.config,該認證是和瀏覽器重定向與人體的服務。我還可以看到SimpleWebToken處理程序得到了實例化,並且正在請求標記類型URI。但幾乎所有這一切都發生了。沒有實際的令牌處理驗證以及任何令人滿意的操作。

這是獲取發送到服務(解析後)的令牌。

wa=wsignin1.0& 
wresult= 
<t:RequestSecurityTokenResponse 
xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"> 
<t:Lifetime> 
    <wsu:Created 
     xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2013-02-13T23:14:30.159Z</wsu:Created> 
    <wsu:Expires 
     xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2013-02-13T23:24:30.159Z</wsu:Expires> 
</t:Lifetime> 
<wsp:AppliesTo 
    xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> 
    <EndpointReference 
     xmlns="http://www.w3.org/2005/08/addressing"> 
     <Address>http://localhost:56782/Service1.svc</Address> 
    </EndpointReference> 
</wsp:AppliesTo> 
<t:RequestedSecurityToken> 
    <wsse:BinarySecurityToken 
     wsu:Id="uuid:58e2fb15-dd1a-40bd-8ff0-ae24e22e6efe" 
     ValueType="http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0" 
     EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" 
     xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"   xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     BASE64 DATA== 
    </wsse:BinarySecurityToken> 
</t:RequestedSecurityToken> 
<t:TokenType>http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0</t:TokenType> 
<t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType> 
<t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType> 
</t:RequestSecurityTokenResponse> 

服務本身是braindead簡單,具有以下簽名。

[OperationContract] 
[WebInvoke(UriTemplate = "/GetData/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
string GetData(string id); 

任何想法?我一直在驗證uri的主機名,拇指指紋等都是有效的。此外,服務跟蹤不會真正顯示與令牌處理或令牌驗證中的例外相關的任何內容。

不知何故,它似​​乎並沒有傳遞給處理程序。至少所有索賠和其他身份驗證信息都丟失(空)。

如果有人指出我可以在哪裏調試,或者如果我錯過了一些非常明顯的東西(這也可能總是如此),我將不勝感激。

P.S.我知道我可以通過自定義身份驗證模塊來實現它,無論如何,我寧願讓它與WIF一起運行(因爲我已經花了更多時間在這個上,因爲我真的很想,而且我非常固執:p)。

+0

你從哪裏得到你正在使用的SimpleWebTokenHandler?請記住,當ACS通過XML協議(如WS-Trust)發佈SWT時,它將它包裝在元素中,您使用的令牌處理程序必須理解該元素。如果我不得不猜測,你使用的處理程序不會將其識別爲SWT。 – 2013-02-14 01:30:38

+0

嗨,我一直在使用從這裏http://code.msdn.microsoft的示例。com/vstudio/Custom-Token-ddce2f55 現在有趣的部分是,實際用於比較令牌內容和令牌處理程序返回的內容的URI是什麼。 t:TokenType和ValueType都會讓我認爲它是我使用的http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0。 我一直無法獲得任何標記相關的跟蹤或調試都沒有:( – Laazik 2013-02-14 09:13:10

回答

1

洙,奉獻將帶來一個解決方案。儘管我最初認爲這不可能完成,但顯然它確實可以。我會把解決方案放在這裏,也許還有其他人覺得它很有用。

首先,WCF REST服務使用webHttpBinding,根據MS文檔不支持Windows Identity Foundation和管道中的聲明處理。其實它確實如此。不在WCF管道中,而是在Web認證流程中作爲IIS模塊。

首先,您需要將以下模塊添加到Web.config文件中。

<system.webServer> 
    <modules runManagedModulesForAllRequests="true"> 
     <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" preCondition="managedHandler" /> 
     <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" preCondition="managedHandler" /> 
    </modules> 
</system.webServer> 

有一個警告壽。您仍然需要我原始發佈的<configSections>。問題在於,您需要在VisualStudio中將System.IdentyModel*程序集標記爲CopyLocal項(在屬性窗口中)。否則,你會得到一些神祕的異常,無法爲配置部分加載程序集。 NB!只有當你加載這兩個模塊時纔會發生,並且當這些模塊沒有被加載時不會發生。沒有任何意願可以進一步調查這件事情,也許有人知道那裏的原因是什麼。

接下來,如果由於任何原因您計劃使用MS WIF代碼中的SWT令牌處理示例,那麼需要修復一些錯誤,否則令牌解析不會發生,否則您將得到無效的簽名出令牌驗證。

SimpleWebToken.cs你需要修復的SwtBaseTime,因爲它是不正確的初始化和安全令牌創建之後失敗:

從 公共靜態的DateTime SwtBaseTime =新的日期時間(1970年,1,1,0, 0,0,0); //每SWT皮秒 要

public static DateTime SwtBaseTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); // per SWT psec 

SimpleWebTokenHandler.cs你需要解決以下值的外殼:

const string BinarySecurityToken = "binarySecurityToken"; 
const string ValueType = "valueType"; 

const string BinarySecurityToken = "BinarySecurityToken"; 
const string ValueType = "ValueType"; 

個CustomIssuerTokenResolver.cs你需要修復,因爲它是用UTF8字節initalized創建的關鍵,但它應該是怎麼被初始化與解碼的Base64的字節:

key = new InMemorySymmetricSecurityKey(UTF8Encoding.UTF8.FromBase64String(base64Key)); 

key = new InMemorySymmetricSecurityKey(System.Convert.FromBase64String(base64Key)); 

修復了所有這些之後,所有內容都處於適當位置。驗證者和授權者正在被調用,並且聲稱,突然間你有一個WCF服務暴露爲REST端點,並且所有的聲明等也在工作。

0

我覺得你的問題可能與SWTTokenHandler此示例中:http://code.msdn.microsoft.com/vstudio/Custom-Token-ddce2f55

在CanReadToken(),它檢查是否令牌類型SWT的的BinarySecurityToken:

if (reader.IsStartElement(BinarySecurityToken) 
&& (reader.GetAttribute(ValueType) == SimpleWebTokenConstants.ValueTypeUri)) 

但常數BinarySecurityToken定義爲:

const string BinarySecurityToken = "binarySecurityToken"; 

請注意小寫字母「b」。 XML元素區分大小寫,而實際元素是大寫字母「B」的BinarySecurityToken。這將導致處理程序在CanReadToken()中返回false,導致WIF相信它沒有爲此標記類型註冊處理程序。

+0

其實,CanReadToken甚至沒有被調用。這是我的問題。我基本上突出了SWT處理程序中的所有功能,我可以看到構造函數,令牌類型請求和令牌類型的URI請求被調用 所以我在想什麼是管道的一部分我沒有得到正確的請求進入 - 檢查,令牌處理管道initalized - 檢查,令牌驗證 - 不發生,授權和認證組件稱爲 - 檢查,wcf函數調用 - 檢查。 – Laazik 2013-02-15 10:26:43