我有一个WCF服务需要接收客户端凭据,并根据我的auth方法维护某种基于角色的数据。

客户端将驻留在许多不同的系统上,因此,每个客户端将具有唯一的用户ID和pw。

我正在使用basicHttpBinding并阅读了一些文章,比如这篇文章, http://nirajrules.wordpress.com/2009/05/22/username-over-https-custombinding-with-wcf%E2%80%99s-channelfactory-interface/ ,描述了这个过程。

所以我正在寻找的是,如果某人有一个像这样配置的完整客户端/服务器来查看,那么我可以从中获得我自己的解决方案。

我想要做的是在每个请求的标头中传递用户名和密码,在失败时传回某种SecurityTokenValidationException,或者在传递时继续传递。

感谢。

<强>更新

我在客户端和服务器上使用带有以下配置的wsHttpbinding:

  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding" >
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="Basic" />
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </wsHttpBinding>

从客户端调用服务器如下:

ServiceReference1.ServiceClient myClient = new ServiceReference1.ServiceClient();

myClient.ClientCredentials.UserName.UserName = "billuser";
myClient.ClientCredentials.UserName.Password = "mypassword";

Response.Write("Data from WCF Service: " + myClient.GetData(1));

我认为我需要在服务器上连接CustomUsernamePasswordValidator,因为我仍然得到'...无法激活'。错误。

有帮助吗?

解决方案

您是否需要使用basicHttpBinding?该绑定实际上只是为传统的WS-BasicProfile实现(即ASMX)提供支持。如果您的客户端也是.NET / WCF,我强烈建议使用wsHttpBinding,它提供了大量的开箱即用安全选项。您可以使用证书,用户名/密码等与传输和/或消息安全性,而不需要自己编写任何安全性的东西。只需配置即可(CAG)。

服务本身可以通过OperationContext获取安全凭证信息,以防您需要直接从代码访问它。但是,如果您的代码确实需要访问它,我建议编写一个行为以从OperationContext中提取相关信息并将其放在更具特定于应用程序的内容中,这样您就不必在需要访问的任何位置引用System.ServiceModel有关OperationContext的信息。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top