码迷,mamicode.com
首页 > Windows程序 > 详细

C# 调用webservice错误An error was discovered processing the <wsse:Security> header

时间:2018-07-03 15:19:24      阅读:779      评论:0      收藏:0      [点我收藏+]

标签:ado   错误   tom   string   asi   add   bind   serialize   process   

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

namespace WebApplication1
{
public class SecurityHeader : MessageHeader
{
private readonly UsernameToken _usernameToken;

public SecurityHeader(string id, string username, string password)
{
_usernameToken = new UsernameToken(id, username, password);
}

public override string Name
{
get { return "Security"; }
}

public override string Namespace
{
get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
}

protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
XmlSerializer serializer = new XmlSerializer(typeof(UsernameToken));
serializer.Serialize(writer, _usernameToken);
}
}


[XmlRoot(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class UsernameToken
{
public UsernameToken()
{
}

public UsernameToken(string id, string username, string password)
{
Id = id;
Username = username;
Password = new Password() { Value = password };
}

[XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")]
public string Id { get; set; }

[XmlElement]
public string Username { get; set; }

[XmlElement]
public Password Password { get; set; }
}

public class Password
{
public Password()
{
Type = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";
}

[XmlAttribute]
public string Type { get; set; }

[XmlText]
public string Value { get; set; }
}
}

 

调用

void Test(){

  

ServiceReference1.recvCustomer rc = new ServiceReference1.recvCustomer
{
reqInfo = "123",
reqData = "456"
};
ServiceReference1.WS_ESB_CustomerRecvPortTypeClient client = new ServiceReference1.WS_ESB_CustomerRecvPortTypeClient();


using (new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageHeaders.Add(
new SecurityHeader("UsernameToken-49", "username", "password"));
var ss= client.recvCustomer(rc);
}

}

 

 

第二种方法:

web.config直接配置

<client>
<endpoint address="http://61.191.200.162:5888/services/WS_ESB_CustomerRecv"
binding="customBinding" bindingConfiguration="WS_ESB_CustomerRecvSoap12Binding"
contract="ServiceReference1.WS_ESB_CustomerRecvPortType" name="WS_ESB_CustomerRecvHttpSoap12Endpoint">

<headers>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<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">wsse:Password@NX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</headers>
</endpoint>
</client>

C# 调用webservice错误An error was discovered processing the <wsse:Security> header

标签:ado   错误   tom   string   asi   add   bind   serialize   process   

原文地址:https://www.cnblogs.com/zxk5625/p/9257671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!