我已按照 msdn 的建议启用了 ASP.Net 身份验证服务。然后,我尝试通过控制台应用程序或 winforms 应用程序使用该服务(通过向本地 WCF 服务添加服务引用)。我正在执行自定义身份验证和传输安全性(因此我正在处理 Global.asax
中的 AuthenticationService.Authenticating
事件,该事件工作正常)。
身份验证本身工作正常,但通过添加服务引用创建的代理不包含 CookieContainer
属性。当我尝试将 cookie token 传递给需要身份验证的后续服务时,这显然是一个问题。
此外,在下面的客户端代码中,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 session ,并且正在设置 cookie 并将其发送回客户端。但是没有 CookieContainer 我该怎么办?
最佳答案
When this option is enabled the client will make sure all cookies received from a given web service are stored and properly sent on each subsequent request in a transparent fashion. But there is a catch: the cookie is only handled in the conversation with one web service. What if you need to send the same cookies to different web services?
如果您需要将相同的 cookie 发送到多个服务,请阅读本文:http://megakemp.wordpress.com/2009/02/06/managing-shared-cookies-in-wcf/
关于WCF 身份验证服务代理 - CookieContainer 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11402970/