C # Call WebService error An error is discovered processing the <wsse:Security> header

Source: Internet
Author: User

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

Call

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

}

The second method:

Web. config Direct configuration

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

<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:[email protected]</wsse:password>
</wsse:UsernameToken>
</wsse:Security>
</endpoint>
</client>

C # Call WebService error An error is discovered processing the <wsse:Security> header

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.