4

WS2007FederationHttpBinding を使用する WCF サービスから生成されたサービス参照を持つ WinForms アプリがあります。以下が機能しない理由がわかりません。

私の WinForms アプリは、BearerKey タイプのトークンを処理するように設定された Thinktecture.IdentityServer を使用する WCF サービスを呼び出しています。

クライアントから有効なアクセス トークンを取得し、次の呼び出しを行います。

    private static void CallServiceReference(SecurityToken token)
    {
        ServiceReference1.ClaimsServiceContractClient svcRef = new ServiceReference1.ClaimsServiceContractClient();

        svcRef.ChannelFactory.Credentials.SupportInteractive = false;
        svcRef.ChannelFactory.CreateChannelWithIssuedToken(token);
        var claims = svcRef.GetClaims(); 
    }

サービス参照用の winforms クライアント app.config は次のとおりです。

<system.serviceModel>
      <bindings>
              <ws2007FederationHttpBinding>
                      <binding name="WS2007FederationHttpBinding_ClaimsServiceContract">
                              <security mode="TransportWithMessageCredential">
                                      <message establishSecurityContext="false" issuedKeyType="BearerKey">
                                              <issuer address="https://identity.MyCo.com/issue/wsfed" binding="ws2007HttpBinding"
                                                      bindingConfiguration="https://identity.MyCo.com/issue/wstrust/mixed/username" />
                                              <issuerMetadata address="https://identity.MyCo.com/issue/wstrust/mex" />
                                              <tokenRequestParameters>
                                                      <trust:SecondaryParameters xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
                                                              <trust:KeyType xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://docs.oasis-open.org/ws-sx/ws-trust/200512/Bearer</trust:KeyType>
                                                              <trust:CanonicalizationAlgorithm xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://www.w3.org/2001/10/xml-exc-c14n#</trust:CanonicalizationAlgorithm>
                                                              <trust:EncryptionAlgorithm xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">http://www.w3.org/2001/04/xmlenc#aes256-cbc</trust:EncryptionAlgorithm>
                                                      </trust:SecondaryParameters>
                                              </tokenRequestParameters>
                                      </message>
                              </security>
                      </binding>
              </ws2007FederationHttpBinding>
              <ws2007HttpBinding>
                      <binding name="https://identity.MyCo.com/issue/wstrust/mixed/username">
                              <security mode="TransportWithMessageCredential">
                                      <transport clientCredentialType="None" />
                                      <message clientCredentialType="IssuedToken" establishSecurityContext="false" />
                              </security>
                      </binding>
              </ws2007HttpBinding>
      </bindings>
      <client>
              <endpoint address="https://roadie/WebTest/service.svc" binding="ws2007FederationHttpBinding"
                      bindingConfiguration="WS2007FederationHttpBinding_ClaimsServiceContract"
                      contract="ServiceReference1.ClaimsServiceContract" name="WS2007FederationHttpBinding_ClaimsServiceContract" />
      </client>
  </system.serviceModel>

サービス呼び出し (svcRef.GetClaims()) を実行しようとすると、次のエラーが発生します。

「セキュリティ トークン発行者のアドレスが指定されていません。ターゲット ' https://identity.MyCo.com/issue/wsfed 'のバインディングで明示的な発行者アドレスを指定する必要があります。または、資格情報でローカル発行者アドレスを構成する必要があります。 ."

このエラーは不完全で紛らわしいですが、設定で発行者が指定されているようです!

最後に、WCF サービスと Identity サービスが有効であることはわかっています。これは、カスタム ChannelFactory を使用してすべて正常に機能し、トークンを適用するのとまったく同じ方法を使用しているためです。

var channel = factory.CreateChannelWithIssuedToken(token);

しかし、私の要件は、生成された ServiceReference を使用することです。:(

4

3 に答える 3

3

作成したチャネルは次のように使用する必要があります。

private static void CallServiceReference(SecurityToken token)
{
    ServiceReference1.ClaimsServiceContractClient svcRef = 
        new ServiceReference1.ClaimsServiceContractClient();

    svcRef.ChannelFactory.Credentials.SupportInteractive = false;
    var svcChannel = svcRef.ChannelFactory.CreateChannelWithIssuedToken(token);
    var claims = svcChannel.GetClaims();
}
于 2014-03-26T09:17:11.923 に答える
0

サービス参照から生成されたプロキシを使用できる唯一の方法は、クライアントがトークンを自動的に要求するように構成し、サービス呼び出しを行う前に、作成したプロキシ インスタンスに適切な ClientCredentials プロパティを設定することだと思います。

私が取り組んでいるプロジェクトのクライアントにキャッシュされている発行済みトークンを使用していますが、説明したようにチャネルファクトリ CreateChannelWithIssuedToken を使用する必要があります。

ところで、これは .NET 4.0 で WIF を使用する場合です。.NET 4.5 で実行している場合は、他のオプションがあるかもしれません。

于 2013-02-13T21:43:19.683 に答える
0

参照されているクライアント スタブ DLL がない場合は、リフレクションを使用して動的にロードして呼び出すことができます。発行されたトークンでクライアントを作成するためのリフレクション バージョンを次に示します。

//Our goal is to get a client obj from reflection
//var obj = client.ChannelFactory.CreateChannelWithIssuedToken(token);

double value1 = 2;
double value2 = 3;
//this line needs to be changed to be created from reflection if needed
CalculatorClient cc = new CalculatorClient("WS2007FederationHttpBinding_ICalculator"); var pi = cc.GetType().GetProperty("ChannelFactory"); //proprety 
var factoryObj = pi.GetValue(cc);
var mi = factoryObj.GetType().GetMethod("CreateChannelWithIssuedToken", new Type[] { typeof(SecurityToken)}); //method info
var clientObj = mi.Invoke(factoryObj, new object[] { token });
mi = clientObj.GetType().GetMethod("Add"); //another method info
var result = mi.Invoke(clientObj, new Object[] { 2, 3 });
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, Convert.ToDouble(result));
于 2019-08-05T03:27:06.117 に答える