WCF身份验证服务代理 - CookieContainer不可用

时间:2012-07-09 20:54:27

标签: wcf authentication wcf-security

我已按照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>

编辑:我应该添加的其他内容,我做了一个调用Login方法的服务的Fiddler会话,并且正在设置cookie并将其发送回客户端。但是我应该怎么做没有CookieContainer?

4 个答案:

答案 0 :(得分:2)

  

启用此选项后,客户端将确保从给定的Web收到所有cookie   服务以透明的方式存储并在每个后续请求中正确发送。   但有一个问题:cookie仅在与一个Web服务的对话中处理。   如果您需要将相同的cookie发送到不同的Web服务,该怎么办?

如果您需要将相同的Cookie发送到多个服务,请阅读以下文章:http://megakemp.wordpress.com/2009/02/06/managing-shared-cookies-in-wcf/

答案 1 :(得分:0)

您需要配置绑定以允许cookie。

<system.ServiceModel>
     <bindings>
            <basicHttpBinding allowCookies="true">
     </bindings>

答案 2 :(得分:0)

显然,在客户端向WCF服务添加服务引用(.Net 3.5+)时,代理类派生自System.ServiceModel.ClientBase。此类没有CookieContainer属性(因为ClientBase支持不具有cookie概念的非HTTP协议)。

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

我可以添加一个Web引用,它将使用.Net 2.0代理类(并且CookieContainer属性已公开)http://msdn.microsoft.com/en-us/library/bb628649.aspx。但我很可能会完全重新审视我的方法,并使用自定义标题和服务行为来实现我的目标。

答案 3 :(得分:0)

另一种选择是访问基础渠道的cookie容器,如下所示:

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

要使上述经理出现,您需要将AllowCookies设置为true,例如:

<system.ServiceModel>
    <bindings>
        <basicHttpBinding allowCookies="true">
    </bindings>
相关问题