使用PowerShell的WCF消息级别用户凭据

时间:2013-12-13 21:17:57

标签: wcf web-services powershell

是否有某种方法可以使用PowerShell连接到具有消息级别用户凭据的Web服务。

我有一个经过测试的Web服务,它可以很好地与我连接的C#程序一起使用,如下所示:

DEVService.ServerLinkClient webService = new DEVService.ServerLinkClient("ServerLinkEndPoint");
webService.ClientCredentials.UserName.UserName = "*****";
webService.ClientCredentials.UserName.Password = ""*****";

Web服务的配置如下:

<bindings>
  <basicHttpBinding>
    <binding name="ServerLinkBinding" maxBufferSize="2097152" maxReceivedMessageSize="2097152" 
             maxBufferPoolSize="2097152" sendTimeout="00:02:00" receiveTimeout="00:02:00">
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

<services>  
  <service name="APOServiceLibrary.ServerLink"
           behaviorConfiguration="SecureBehaviourServerLink">
    <endpoint name="ServerLinkEndPoint"
              address=""
              binding="basicHttpBinding"
              bindingConfiguration="ServerLinkBinding"
              contract="APOServiceLibrary.IServerLink">
    </endpoint>
    <endpoint address="mexServerLink"
              binding="mexHttpsBinding"
              contract="IMetadataExchange">
    </endpoint>
  </service>
</services>


<behaviors>
  <serviceBehaviors>    
    <behavior name="SecureBehaviourServerLink">
      <serviceMetadata httpsGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False"/>
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="APOServiceLibrary.ConnectionSecurityServerLink, APOServiceLibrary"/>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

我以前使用PowerShell连接到一个简单的Web服务,但该服务设置为使用Windows身份验证。在这种情况下,传输层只是基本安全的,并且消息期望某个用户名和密码。

这是我试图用来从Web服务获取数据集的脚本:

#setup the credentials to connect 
$securePasswd = ConvertTo-SecureString "*******" -AsPlainText -Force
$tempCredentials = New-Object System.Management.Automation.PSCredential (""*******" ", $securePasswd)

# Connect to the web services
$web_serv = New-WebServiceProxy -uri https://127.0.0.1/ServerLink.svc -Credential $tempCredentials

# get some data
$test_data = New-Object System.Data.DataSet
$test_data = $web_serv.getDataSet()

但是,当我尝试使用凭据进行连接并尝试运行命令时,我收到以下错误:

Exception calling "getDataSet" with "0" argument(s): "An error occurred when verifying security for the message."

任何帮助都会很棒! 或者甚至知道是否可以将某种消息级别的用户名和密码放入powershell连接中,这样做会很好!

1 个答案:

答案 0 :(得分:0)

我发现了这个: http://social.technet.microsoft.com/Forums/windowsserver/en-US/54d89b12-8623-47d1-8873-b4ba7927a4a5/powershell2-newwebserviceproxy-soap-security-header?forum=winserverpowershell

基本上建议powershell New-WebServiceProxy命令行开关不够详细,无法处理这种复杂程度。

为此,我通过将Web服务更改为仅使用传输安全性解决了我的问题,如下所示:

<binding name="ServerLinkBinding" maxBufferSize="2097152" maxReceivedMessageSize="2097152" 
         maxBufferPoolSize="2097152" sendTimeout="00:02:00" receiveTimeout="00:02:00">
  <security mode="Transport" />
</binding>

并且要求为每个方法提供用户名和密码(在我的情况下,由于方法数量有限,它可以工作)。