Web Services 具有安全性的"身份验证优先“

aij0ehis  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(142)

我正试图连接到WS与WS安全。
我用SOAP UI测试了一下:

很好用。
我确实尝试了很多C#代码的方法,但总是有例外:例如这个方法:

using (var client = new DocumentWSWs.DocumentUploadWSClient())
{

    DocumentWSWs.StatusRequest currentStatusRequest = new DocumentWSWs.StatusRequest();

    client.ClientCredentials.UserName.UserName = UserNameWs;
    client.ClientCredentials.UserName.Password = PasswordWs;
    SetTlsSecurite();

    DocumentWSWs.StatusResponse getCurrentStatusResponse = client.GetStatus(currentStatusRequest);
}

使用app.config:

<endpoint address="https://*************/DocumentUploadWS.wsdl"
            binding="basicHttpBinding" bindingConfiguration="DocumentUploadWSSoap11"
            contract="DocumentWSWs.DocumentUploadWS" name="DocumentEndPoint" >
            <headers>
                <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                    <wsse:UsernameToken  Id="UsernameToken-49">
                        <wsse:Username>*******</wsse:Username>
                        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">***************</wsse:Password>
                    </wsse:UsernameToken>
                </wsse:Security>
            </headers>
    </endpoint>

    <binding name="DocumentUploadWSSoap11">
                <security mode="Transport">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
    </binding>

但是我得到了:* HTTP请求没有被“匿名”客户端身份验证方案授权 *
我尝试了security mode=“TransportWithMessageCredential”
但是我得到了这个错误:

  • 在客户端配置服务ServiceModel中找不到引用协定“DocumentaladWSSoap11.DocumentaladWS”的默认终结点元素。可能是没有找到应用程序的配置文件,或者在客户端元素中找不到与协定对应的终结点元素。*

尽管检查了许多博客和论坛,我发现了错误。或者博客不清楚如何使用给出的代码。
谁能给予我正确的方向或方法来调用这个WS
谢谢

webghufk

webghufk1#

证件被正确地拿走了
以编程方式配置端点和绑定(如果您不想使用app.config中的配置)。您可以像这样创建自定义绑定和端点:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

EndpointAddress endpointAddress = new EndpointAddress("https://*************");

using (var client = new DocumentWSWs.DocumentUploadWSClient(binding, endpointAddress))
{
    client.ClientCredentials.UserName.UserName = UserNameWs;
    client.ClientCredentials.UserName.Password = PasswordWs;

    // Now, you can call the service methods.
}

现在拨打服务电话:

DocumentWSWs.StatusRequest currentStatusRequest = new DocumentWSWs.StatusRequest();
DocumentWSWs.StatusResponse getCurrentStatusResponse = client.GetStatus(currentStatusRequest);
  • 编辑-
    如果您只提供了使用带预身份验证的SOAP UI连接到Web服务所需的信息,并且客户端没有提供特定的配置详细信息,则可以尝试尽可能接近地模仿C#客户端中的SOAP UI配置。
    1.在SOAP UI中,打开成功连接到Web服务的项目和请求
    1.捕获SOAP UI中使用的配置设置
    1.在SOAP UI中,检查如何为身份验证配置用户名和密码。注意身份验证方法(例如,基本身份验证)。
    在检查任何其他配置后,配置客户端。下面是一个例子:
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

EndpointAddress endpointAddress = new EndpointAddress("https://your-service-endpoint-url");

using (var client = new DocumentWSWs.DocumentUploadWSClient(binding, endpointAddress))
{
    client.ClientCredentials.UserName.UserName = "your-username";
    client.ClientCredentials.UserName.Password = "your-password";

    // Make the service call here
}

相关问题