2

msdnの推奨に従って、ASP.Net認証サービスを有効にしました。次に、コンソールアプリまたはwinformsアプリを介してサービスを使用しようとしています(ローカルのWCFサービスにサービス参照を追加することにより)。私はカスタム認証とトランスポートセキュリティを行っています(したがって、正常に機能するAuthenticationService.Authenticatingイベントを処理してGlobal.asaxいます)。

CookieContainer認証自体は正常に機能しますが、サービス参照を追加して作成されたプロキシにはプロパティが含まれていません。これは、認証を必要とする後続のサービスにCookieトークンを渡そうとすると明らかに問題になります。

また、次のクライアントコードでは、IsLoggedIn()falseが返されます。これは、Cookieコンテナが存在しないことに関連していると思います。

ServiceReference1.AuthenticationServiceClient client = 
                  new ServiceReference1.AuthenticationServiceClient();
bool isLoggedIn = client.Login("test", "test", "", true); //returns TRUE
bool check = client.IsLoggedIn(); //returns FALSE

これがサービスの私のweb.configです:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <system.web.extensions>
    <scripting>
      <webServices>
        <authenticationService enabled="true"
           requireSSL = "false"/>
      </webServices>
    </scripting>
  </system.web.extensions>
  <system.serviceModel>
    <services>
      <service name="System.Web.ApplicationServices.AuthenticationService"
          behaviorConfiguration="AuthenticationServiceTypeBehaviors">
        <endpoint contract="System.Web.ApplicationServices.AuthenticationService"
          binding="basicHttpBinding"
          bindingConfiguration="userHttps"
          bindingNamespace="http://asp.net/ApplicationServices/v200"/>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="userHttps" allowCookies="true">
          <!--<security mode="Transport" />-->
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="AuthenticationServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
</configuration>

編集:他に追加する必要があるものがあります。メソッドを呼び出すサービスのフィドラーセッションを実行しました。CookieLoginが設定され、クライアントに返送されています。しかし、CookieContainerなしで何をすべきでしょうか?

4

4 に答える 4

2

このオプションを有効にすると、クライアントは特定の Web サービスから受信したすべての Cookie が保存され、後続の各要求で透過的に適切に送信されるようになります。ただし、問題があります。Cookie は、1 つの Web サービスとの会話でのみ処理されます。同じ Cookie を異なる Web サービスに送信する必要がある場合はどうすればよいでしょうか?

同じ Cookie を複数のサービスに送信する必要がある場合は、次の記事をお読みください: http://megakemp.wordpress.com/2009/02/06/managing-shared-cookies-in-wcf/

于 2012-07-09T21:41:46.143 に答える
0

どうやら、クライアント上のWCFサービスにサービスリファレンス(.Net 3.5+)を追加する場合、プロキシクラスはから派生しSystem.ServiceModel.ClientBaseます。このクラスにはCookieContainerプロパティがありません(ClientBaseがCookieの概念を持たない非HTTPプロトコルをサポートしているため)。

http://netpl.blogspot.com/2011/11/managing-cookies-in-wcf-client.html

代わりに、.Net 2.0プロキシクラスを使用する(そしてCookieContainerプロパティが公開されている) Webリファレンスを追加することもできますhttp://msdn.microsoft.com/en-us/library/bb628649.aspx。しかし、私はおそらく私のアプローチを完全に再検討し、カスタムヘッダーとサービス動作を使用して目標を達成します。

于 2012-07-10T14:44:24.157 に答える
0

もう 1 つのオプションは、次のように基になるチャネルの Cookie コンテナーにアクセスすることです。

var cookieManager = client.InnerChannel.GetProperty<IHttpCookieContainerManager>();
cookieManager.CookieContainer.Add(new Cookie(....));

上記のマネージャーが存在するためには、AllowCookies を true に設定する必要があります。

<system.ServiceModel>
    <bindings>
        <basicHttpBinding allowCookies="true">
    </bindings>
于 2015-08-21T07:20:01.757 に答える
0

Cookie を許可するようにバインディングを構成する必要があります。

<system.ServiceModel>
     <bindings>
            <basicHttpBinding allowCookies="true">
     </bindings>
于 2012-07-09T21:18:11.953 に答える