我所做的是: 我創建了自己的響應消息版本,其中包含創建GenericXmlSecurityToken所需的位。這通常是從WSTrustChannel返回的,所以它看起來是正確的。謝天謝地,包裝JWT的GenericXmlSecurityToken的大部分參數都是空的;我只需要序列化令牌,使用服務中的JWTSecurityTokenHandler上的WriteToken和validFrom和validTo值序列化。
客戶機代碼:在web.config的
XmlElement element = document.CreateElement("wsse", "BinarySecurityToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
element.SetAttribute("ValueType", "urn:ietf:params:oauth:token-type:jwt");
element.SetAttribute("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
UTF8Encoding encoding = new UTF8Encoding();
element.InnerText = Convert.ToBase64String(encoding.GetBytes(jwtToken));
GenericXmlSecurityToken token = new GenericXmlSecurityToken(
element,
null,
validFrom,
validTo,
null,
null,
null);
var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.IssuedKeyType = SecurityKeyType.BearerKey;
binding.Security.Message.EstablishSecurityContext = false;
binding.Security.Message.IssuedTokenType = "urn:ietf:params:oauth:token-type:jwt";
var factory2 = new ChannelFactory<IService1>(binding, new EndpointAddress("https://localhost:44300/Service1.svc"));
factory2.Credentials.SupportInteractive = false;
factory2.Credentials.UseIdentityConfiguration = true;
var proxy = factory2.CreateChannelWithIssuedToken(token);
var info = proxy.DoWork();
相關位:
的結合:
<ws2007FederationHttpBinding>
<binding>
<security mode="TransportWithMessageCredential">
<message issuedKeyType="BearerKey" establishSecurityContext="false" issuedTokenType="urn:ietf:params:oauth:token-type:jwt"/>
</security>
</binding>
</ws2007FederationHttpBinding>
的identityModel部分:
<system.identityModel>
<identityConfiguration>
<audienceUris>
<add value="--audienceUri--"/>
</audienceUris>
<securityTokenHandlers>
<add type="--namespace--.CustomJWTSecurityTokenHandler, --my dll--" />
<securityTokenHandlerConfiguration>
<certificateValidation certificateValidationMode="PeerTrust"/>
</securityTokenHandlerConfiguration>
</securityTokenHandlers>
<issuerNameRegistry>
<trustedIssuers>
<add name="--issuer--" thumbprint="--thumbprint--"/>
</trustedIssuers>
</issuerNameRegistry>
</identityConfiguration>
</system.identityModel>
而CustomJWTSe curityTokenHandler從這個問題(只有validIssuer部分是需要爲我的方案):How to configure MIcrosoft JWT with symmetric key?
我還沒有看到其他地方使用issuedTokenType屬性,但我發現這是至關重要的讓我的代碼工作。沒有它,我得到這個錯誤:「MessageSecurityException:無法找到'Microsoft.IdentityModel.Tokens.JWT.JWTSecurityToken'令牌類型的令牌認證程序。該類型的令牌不能根據當前的安全設置被接受。」
這可能是矯枉過正的解決方案,但我認爲它最小化的自定義代碼的數量和集中它的地方,我感覺更舒服。
由於雙方user2338856和leastprivilege你爲我中途有
爲什麼這麼複雜 - 簡單地添加一個操作爲gettoken(或東西),並返回一個字符串... – leastprivilege 2013-05-01 07:11:36
的問題是如何把這個字符串,並把它變成一個SecurityToken傳遞給CreateChannelWithIssuedToken,而無需將JWT令牌和處理程序與客戶端一起傳送。您是否建議僅傳遞令牌字符串作爲標題,然後使用行爲來手動處理令牌? – 2013-05-01 18:25:22