【问题标题】:Using a particular webservice in Powershell在 Powershell 中使用特定的 Web 服务
【发布时间】:2014-05-21 06:16:25
【问题描述】:

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

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

[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 获取 web 服务的配置时,它给了我以下配置

<?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:更新以反映最新脚本

【问题讨论】:

    标签: web-services powershell


    【解决方案1】:

    您正在尝试从 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

    【讨论】:

    • 谢谢!我通过管理控制台导入和导出到 Cer 文件。我的问题现在反映了对脚本的建议更改不幸的是,结果完全一样您的这一行我不明白“不确定 Get-Certificate cmdlet,但您也可以使用 -UseDefaultCredential 选项。”
    • @Sandro 看这里 technet.microsoft.com/en-us/library/hh849841.aspx,让我知道 -UseDefaultCredential 选项是否适合您。换句话说,试试这条线 $proxy = New-WebServiceProxy -uri url/webService/platform/CoreWebService.svc?wdsl -UseDefaultCredential 并删除这条线: $proxy.Credentials = Get-Credential
    • 您好!我尝试了上述方法,但没有奏效。我可能会误解一些东西:需要提供的凭据是来自远程 Web 服务的登录数据,对吧?我的 Windows 帐户没有关联,是吗?而且我相信 .config 文件中的客户端证书会告诉我在另一个中寻找什么,以确保我与正确的 Web 服务进行通信。正确的?无论哪种方式:我还不在那里。我还需要做些什么来设置正确的端点?
    • 哦等等。您可以使用证书存储中的证书从浏览器打开该 Web 服务 URL 吗?如果不能,则必须提供用户凭据。 Get-Credential 是一个获取当前 Windows 用户凭据的 cmdlet。顺便说一句,为什么 URL 的最后 4 个字母是 WDSL 而不是 WSDL?
    • 回到问题:WDSL 是一个错字(很好,在问题中更正),但更正它并没有改变任何有关功能的东西。 “使用证书在浏览器中打开 Web 服务 URL”是什么意思?我可以在没有 WSDL 的情况下浏览到上面的 URL,它会给我一个关于如何使用它的描述,在 ?wsdl 下我会得到一个 XML 文件。支持声称,他们正在使用 HTTP(通过一些内部措施使其安全),因此不需要证书。我正在向他们澄清这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多