为什么来自 C# 应用程序的 Web 服务调用返回 asp.net NULL

本文关键字:返回 调用 asp net NULL 服务 Web 应用程序 为什么 | 更新日期: 2023-09-27 18:36:22

在我的应用程序中,我正在使用一个 Web 服务,并且在调用 Web 服务函数时,我正在使用自定义类在 SOAP 请求的标头中使用用户名和密码创建用户名令牌。

引用我为制作标头而制作的自定义类:- https://msdn.microsoft.com/en-us/library/ms730868(v=vs.110).aspx

但是当我使用 xmlns:

wsse 时,我遇到了内部错误,当我使用 xmlns:o对于标头中的<security>标签,我得到空响应。

如果我在配置中添加用户名和密码,则相同的应用程序工作正常

<headers>
    <wsse:Security mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>username</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
   </headers>

我想更改对同一 Web 服务的不同网络调用的用户名和密码,所以我正在使用自定义类并生成标头,不幸的是它变成了 wcf 身份验证,其中安全标记如下所示:-

      <o:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <o:Username></o:Username>
        <o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"></o:Password>
      </o:UsernameToken>

这为我的函数调用返回 null。知道我怎样才能让它工作吗?任何建议将不胜感激。

注意:它说Microsoft.net弃用了wse的使用,并更新为wcf身份验证。但是它在配置文件中使用wsse和用户名和密码对我有用,但是我无法更改Web服务中不同函数调用的用户名和密码。

我初始化Web服务并调用该方法的代码如下:-

  string endpointUri = @"https://my endpoint url";
                var endpoint = new System.ServiceModel.EndpointAddress(endpointUri);
                // Create binding using HTTPS
                var securityElement = System.ServiceModel.Channels.SecurityBindingElement.CreateUserNameOverTransportBindingElement();
                securityElement.IncludeTimestamp = true;
                securityElement.EnableUnsecuredResponse = true;
                securityElement.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256;
                securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
                var encodingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement(System.ServiceModel.Channels.MessageVersion.Soap11, System.Text.Encoding.UTF8);
                var transportElement = new System.ServiceModel.Channels.HttpsTransportBindingElement();
                transportElement.MaxReceivedMessageSize = 20000000;
                var binding = new System.ServiceModel.Channels.CustomBinding(securityElement, encodingElement, transportElement);
                QTWebSvcsClient client = new QTWebSvcsClient(binding, endpoint);
                //==========================================================================================================================
                client.ChannelFactory.Endpoint.Behaviors.Remove<ClientCredentials>();
                client.ChannelFactory.Endpoint.Behaviors.Add(new MyClientCredetnials());
                client.ClientCredentials.UserName.UserName = userName + "@" + comp;
                client.ClientCredentials.UserName.Password = password;
                getDriverDetailsRequest request = new getDriverDetailsRequest();
                request.driverId = "Driver1";
                getDriverDetailsResponse response = new getDriverDetailsResponse();
                response.getDriverDetailsReturn = client.getDriverDetails(request.driverId);

这会向我的代码返回 null,并且我在此处引用 null 对象时出现异常

Console.WriteLine(response.getDriverDetailsReturn.driverName);

为什么来自 C# 应用程序的 Web 服务调用返回 asp.net NULL

我决定不遵循艰难的方式,而是可以实现我真正的要求 - 通过程序动态更改 Web 安全凭据。

我只是使用了在向 C# 应用程序添加 Web 服务引用时自动创建的配置文件代码。

然后在代码中我起诉

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
                binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
                CustomBinding customBinding = new CustomBinding(binding);
                SecurityBindingElement element = customBinding.Elements.Find<SecurityBindingElement>();
                element.IncludeTimestamp = false;
                EndpointAddress epadd = new EndpointAddress("https://mycompany.ca:113/tracsWebWS/services/TWebSvcs");
                TWebSvcsClient client = new TWebSvcsClient(customBinding, epadd);
                client.ClientCredentials.UserName.UserName = userName;
                client.ClientCredentials.UserName.Password = password;

然后通过 Web 服务调用函数给了我正在寻找的答案。

谢谢所有帮助我提出建议的人。