【问题标题】:Pass token from MVC to WCF service将令牌从 MVC 传递到 WCF 服务
【发布时间】:2014-09-24 23:22:12
【问题描述】:

我有一个 MVC 应用程序与 ACS 通信以获取用于身份验证的令牌。这是一个基于索赔的应用程序。这工作得很好。

我正在尝试从 MVC 调用 WCF 服务,一旦通过相同的方式进行身份验证,以便我可以使用相同的声明进行授权。

MVC代码如下

    var context = (BootstrapContext)identity.BootstrapContext;
        var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.Message);
        binding.Security.Message.IssuedKeyType = SecurityKeyType.SymmetricKey;
        binding.Security.Message.EstablishSecurityContext = false;
        binding.Security.Message.IssuerBinding = new WS2007FederationHttpBinding();
        EndpointAddress acsEndPoint = 
    new EndpointAddress("https://ACS namespace/v2/wsfederation");
        binding.Security.Message.IssuerAddress = acsEndPoint;
        binding.Security.Message.IssuedTokenType = "urn:ietf:params:oauth:token-type:jwt";
        ChannelFactory<IService1> factory = 
new ChannelFactory<IService1>(binding, new EndpointAddress("https://localhost/TestWCF/Service1.svc"));
        factory.Credentials.SupportInteractive = false;
        factory.Credentials.UseIdentityConfiguration = true;


        var proxy = factory.CreateChannelWithIssuedToken(context.SecurityToken);
        proxy.GetData(1);

WCF 网页配置如下

<system.serviceModel>
    <services>
  <service name="TestWCF.Service1">
    <endpoint address="" behaviorConfiguration="webHttpAutoFormat" binding="ws2007FederationHttpBinding"    bindingConfiguration="secureHttpBinding" contract="TestWCF.IService1"/>
    <endpoint address="soap" binding="basicHttpBinding" contract="TestWCF.IService1" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
  <bindings>
  <ws2007FederationHttpBinding>
    <binding name="secureHttpBinding">
      <security mode="None">
        <message establishSecurityContext="false" issuedKeyType="SymmetricKey" issuedTokenType="urn:ietf:params:oauth:token-        type:jwt">
                      <issuerMetadata address="https://ACS namespace/v2/wstrust/mex"></issuerMetadata>
        </message>
      </security>
    </binding>
  </ws2007FederationHttpBinding>
</bindings>
  <behaviors>
  <serviceBehaviors>
    <behavior>

      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>

      <serviceDebug includeExceptionDetailInFaults="false"/>
      <serviceCredentials useIdentityConfiguration="true"></serviceCredentials>
      <serviceAuthorization principalPermissionMode="Always" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="webHttpAutoFormat">
    </behavior>
  </endpointBehaviors>
  </behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
  <serviceActivations>
    <add relativeAddress="Service1.svc" service="TestWCF.Service1" />
  </serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>

请注意,我的 WCF 服务不是 HTTPS,而且我使用的是来自 ACS 的 JWT 令牌。没有证书。

我得到以下错误

提供的 URI 方案“https”无效;预期的“http”。 参数名称:通过

谁能帮忙?

【问题讨论】:

    标签: wcf token jwt


    【解决方案1】:

    您当前正在初始化您的绑定

    var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.Message)
    

    试试改成

    var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.TransportWithMessageCredential)
    

    发件人(MSDN - WS Transport With Message Credential):

    默认情况下,wsHttpBinding 绑定提供 HTTP 通信。 为传输安全配置时,绑定支持 HTTPS 沟通。 HTTPS 提供机密性和完整性保护 对于通过网络传输的消息。然而集 可用于验证的验证机制 服务的客户端仅限于 HTTPS 传输支持的内容。 Windows Communication Foundation (WCF) 提供了一个 TransportWithMessageCredential 安全模式,旨在 克服这个限制。配置此安全模式后, 传输安全用于提供机密性和完整性 用于传输的消息并执行服务 验证。但是,客户端身份验证由 将客户端凭据直接放在消息中。这让你 使用消息安全支持的任何凭证类型 客户端身份验证模式,同时保持性能 运输安全模式的好处。

    你的网络配置应该有这个而不是&lt;ws2007FederationHttpBinding&gt;

      <ws2007FederationHttpBinding>
        <binding name="secureHttpBinding">
          <security mode="TransportWithMessageCredential">
            <message establishSecurityContext="false" issuedKeyType="SymmetricKey" issuedTokenType="urn:ietf:params:oauth:token-        type:jwt">
                          <issuerMetadata address="https://ACS namespace/v2/wstrust/mex"></issuerMetadata>
            </message>
          </security>
        </binding>
      </ws2007FederationHttpBinding>
    

    有关其他信息,另请参阅以下答案:StackOverflow - The provided URI scheme 'https' is invalid; expected 'http'. Parameter name: via

    【讨论】:

    • 我试过了,但是没有用。请注意我没有使用任何证书.. 任何地方都没有 HTTPS
    • mode="TransportWithMessageCredential"... 如果我将模式设置为 None 它可以正常工作,但如果我将其设置为 Message 或 TransportWithMessageCredential 那么我会得到不同的错误。我需要任何类型的证书或任何东西吗?
    • 终于成功了。我们确实需要 .. 除此之外,我们需要保护 WCF (HTTPS),并且我的证书是为我的计算机名颁发的,并且我使用的是本地主机。我也在使用 JWT 令牌,所以我必须在通过 channelfactory 发送之前对其进行序列化。我有整个工作,但不知道如何在这里上传工作解决方案。花了将近 3 天的时间才让它工作......但现在它工作顺利:)
    猜你喜欢
    • 2012-04-10
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多