为什么从asp.net c#应用程序进行的Web服务调用返回null?

安东尼

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

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

但是,当我使用xmlns:wsse时,我会收到内部错误消息,而当我<security>在标题中将xmlns:o用作标签时,我会得到空响应。

如果我在config中添加用户名和密码,则同一个应用程序可以正常工作

<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>

我想将differnetnet调用的用户名和密码更改为相同的Web服务,因此我正在使用自定义类并生成标头,不幸的是,它变成了wcf auhtentication,其中的安全标签如下所示:-

      <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服务中不同函数调用的用户名和密码。

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

  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);

这将空值返回到我的代码,并且在此处引用空对象时出现异常

Console.WriteLine(response.getDriverDetailsReturn.driverName);
安东尼

我决定不遵循严格的方法,而只能满足我的真正要求-通过程序动态更改Web安全凭证。

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

然后在代码中我起诉了

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服务调用该函数给了我所需的答案。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在ASP.NET MVC 4 Web应用程序中未调用我的操作?

使用Kestrel Web服务器而不是IIS的Asp.Net Core应用程序,为什么?

为什么 .Mobile.cshtml 不会在 ASP.NET Core C# Razor Pages Web 应用程序中呈现?

为什么.NET在MVC asp.net应用程序中生成两个web.config文件?

在 ASP.NET Core 托管的 Blazor 应用程序中对服务器进行匿名调用

ASP .Net Core Web 应用程序调用 Web API

在将ASP.NET 4.6应用程序本地部署到IIS 10服务器后,为什么用户不能进行身份验证?

为什么我的ASP.NET 5应用程序不能在IIS 7.5上运行?

为什么ASP.NET Core应用程序无法在同一域的iframe中加载?

为什么我的字体无法在ASP.NET Core MVC应用程序中使用?

当运行asp.net mvc应用程序时,为什么会从kendo ui获取错误?

为什么我无法在 ASP.NET Core 应用程序中添加新视图?

Web服务导致ASP.Net应用程序崩溃

开发ASP.NET WEB API服务应用程序

为什么我的Jquery Ajax调用不起作用?(Asp.net MVC C#)

为什么我的Cookie在C#ASP.NET空Web项目中显示为null?

如何在ASP.NET和C#应用程序之间进行通信

WCF服务应用程序VS ASP.NET Web应用程序?

ASP.nET核心Web应用程序(.NET Core RC2)-为什么head标签中的项目显示在body标签中

如何反编译 ASP.NET / C# Web 应用程序

C#,Asp.Net Web应用程序中出现奇怪的System.StackOverflowException错误

C# 将数据从 Excel 传递回 ASP.NET Web 应用程序中的 Datagrid

ASP.NET Web 应用程序项目仅显示 VB 选项,而不显示 C#

如何使用 asp.net C# 在 Web 应用程序中显示捕获的指纹

Web 应用程序发布失败 - Visual Studio - C# - ASP.NET

Request["key"] 在 C# web 应用程序 (ASP.NET Core) 中不起作用

什么是服务,为什么要在ASP.NET Core中添加它们?

为什么会出现“安装程序安装我的ASP.Net Web应用程序时,安装程序被中断,而xxx可能无法安装”的错误?

如何在ASP.NET Web应用程序中的服务器和客户端之间进行通信?