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

2021-03-31 11:24

阅读:540

标签: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直接配置


binding="customBinding" bindingConfiguration="WS_ESB_CustomerRecvSoap12Binding"
contract="ServiceReference1.WS_ESB_CustomerRecvPortType" name="WS_ESB_CustomerRecvHttpSoap12Endpoint">




UserName
wsse:Password@NX




C# 调用webservice错误An error was discovered processing the header

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

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


评论


亲,登录后才可以留言!