在我的应用程序中,我正在使用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] 删除。
我来说两句