首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JWT SecurityToken交付给WCF客户端

将JWT SecurityToken交付给WCF客户端
EN

Stack Overflow用户
提问于 2013-05-01 12:52:43
回答 2查看 3.3K关注 0票数 1

我需要基于用户名/密码身份验证生成令牌并向客户端颁发令牌。我尝试了几种方法来解决这个问题,但它们都遇到了问题。

我的第一个计划是在我的WCF端点上实现WS-Trust问题。我找到的执行此操作的示例使用:

代码语言:javascript
复制
[OperationContract(Action = "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue",
                   ReplyAction = "http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue")]
Message IssueToken(Message rstMessage);

然而,为了将其集成到.NET框架中,4.5中对WIF4.5的更改破坏了将消息转换为RequestSecurityToken的其余示例代码。WSTrustRequestSerializer似乎可以做到这一点,但它需要WSTrustSerializationContext,并且几乎没有关于如何创建或配置此上下文对象的信息。

我试着简单地将我想要用于SecurityToken类型的JWT序列化为字符串并将其返回给客户端,但看起来将其反序列化为WCF可以使用的SecurityToken将需要我在客户端提供JWTSecurityToken和处理程序,这是我想要避免的事情。虽然WS-Trust绑定似乎以某种方式避开了这一点,并生成了一个GenericXmlSecurityToken,但我似乎找不到如何自己创建一个这样的绑定。

关于如何在WS-Trust中序列化/反序列化RequestSecurityToken和RequestSecurityTokenResponse对象,或者如何在WS-Trust框架外序列化/反序列化令牌,您有什么想法吗?或者其他想法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-02 13:14:43

我所做的是这样的:我创建了自己版本的响应消息,其中包含创建GenericXmlSecurityToken所需的部分。这通常是从WSTrustChannel返回的,所以看起来应该这样做。值得庆幸的是,包装JWT的GenericXmlSecurityToken的大多数参数都是空的;我只需要序列化的令牌(在服务中的JWTSecurityTokenHandler上用WriteToken序列化)以及validFrom和validTo值。

客户端代码:

代码语言:javascript
复制
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();

Web.config的相关部分:

绑定:

代码语言:javascript
复制
<ws2007FederationHttpBinding>
  <binding>
    <security mode="TransportWithMessageCredential">
      <message issuedKeyType="BearerKey" establishSecurityContext="false" issuedTokenType="urn:ietf:params:oauth:token-type:jwt"/>
    </security>
  </binding>
</ws2007FederationHttpBinding>

identityModel部分:

代码语言:javascript
复制
<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>

和这个问题中的CustomJWTSecurityTokenHandler (在我的场景中只需要validIssuer部分):How to configure MIcrosoft JWT with symmetric key?

我还没有看到在其他地方使用的issuedTokenType属性,但我发现它对于让我的代码正常工作是必不可少的。如果没有它,我会得到这样的错误:"MessageSecurityException:找不到'Microsoft.IdentityModel.Tokens.JWT.JWTSecurityToken‘令牌类型的令牌验证器。根据当前的安全设置,该类型的令牌不能被接受。“

这可能是一种过度的解决方案,但我认为它最大限度地减少了自定义代码的数量,并将其集中在我觉得更舒服的地方。

感谢user2338856和至少特权让我走到了半路!

票数 4
EN

Stack Overflow用户

发布于 2013-05-01 17:10:40

AFAIK,你需要一个WSFederationBinding来做这个。开箱即用,这只支持Saml令牌。要让它支持JWT令牌,您需要添加一个能够在WiF管道中处理它的安全令牌处理程序。不幸的是,Microsoft的实验处理程序对配置文件并不友好,因此您需要将其子类化以允许您在config中指定属性。或者,您可以使用ThinkTecture Jwt处理程序,该处理程序在配置中也不是特别容易设置。设置这一切将花费你相当多的时间,我不知道在网络上有任何这样做的例子。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16312907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档