在Powershell中使用特定的Web服务

时间:2014-05-21 06:16:26

标签: web-services powershell

我正在尝试访问Powershell中的Web服务

这是我的代码,包括我收到的错误消息

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$proxy = New-WebServiceProxy -uri http://url/webService/platform/CoreWebService.svc?wdsl

$cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate("test.cer")

$proxy.ClientCertificates.Add($cert)

$proxy.Credentials = Get-Credential

$proxy.WorkspaceList()

#Ausnahme beim Aufrufen von "WorkspaceList" mit 0 Argument(en):  "Logon failed: unknown user name, wrong password or account disabled."
#In Zeile:2 Zeichen:5
#+     $proxy.WorkspaceList()
#+     ~~~~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
#    + FullyQualifiedErrorId : SoapException

多次检查用户名,并在支持时确认它是正确的用户名并且已为其设置帐户。另外:它适用于Visual Studio项目

通过svcutil获取webservice的配置时,它为我提供了以下配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICoreWebServiceBasic" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ICoreWebService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
                <binding name="WSHttpBinding_ICoreWebService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://url/webService/platform/CoreWebService.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICoreWebService"
                contract="ServiceReference1.ICoreWebService" name="WSHttpBinding_ICoreWebService">
                <identity>
                    <certificate encodedValue="certificate string, which I copied into test.pfx, then imported into certificate store and exported as DER encoded cer file" />
                </identity>
            </endpoint>
            <endpoint address="http://url/webService/platform/CoreWebService.svc/wauth"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICoreWebService1"
                contract="ServiceReference1.ICoreWebService" name="WSHttpBinding_ICoreWebService1">
                <identity>
                    <servicePrincipalName value="host/AMAZONA-1AGOCUI" />
                </identity>
            </endpoint>
            <endpoint address="http://url/webService/platform/CoreWebService.svc/basic"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICoreWebServiceBasic"
                contract="ServiceReference1.ICoreWebServiceBasic" name="BasicHttpBinding_ICoreWebServiceBasic" />
        </client>
    </system.serviceModel>
</configuration>

在手册中,他们给出了使用WSHttpBinding_ICoreWebService端点的示例,这也适用于Visual Studio项目。

我的Powershell脚本中缺少什么?

谢谢!

桑德罗

2014-05-22:更新以反映最新脚本

1 个答案:

答案 0 :(得分:0)

您正在尝试从PFX文件而不是CER加载X509Certificate。 PFX是安全证书,需要私钥才能将其保存到商店。

您可以手动或通过代码(使用Import-PfxCertificate)将其添加到证书存储区。然后你可以导出CER证书,最后可以这一行:

$ cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate(“test.cer”)

不确定Get-Certificate cmdlet,但您也可以使用-UseDefaultCredential选项。

这是一个类似的主题:

X509Certificate.CreateFromCertFile - the specified network password is not correct