Skip to content

Instantly share code, notes, and snippets.

@awrowse
Created February 20, 2013 16:12
Show Gist options
  • Save awrowse/4996704 to your computer and use it in GitHub Desktop.
Save awrowse/4996704 to your computer and use it in GitHub Desktop.
WCF SOAP with custom Auth Message Headers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ServiceTest.SMSWS;
using System.ServiceModel.Channels;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Web.Services.Configuration;
using System.Web.Services.Protocols;
using System.Reflection;
using System.ServiceModel.Security;
namespace ServiceTest {
class Program {
static void Main(string[] args) {
Console.WriteLine("Starting Program");
EndpointAddress endPointAddr = new EndpointAddress("https://someservice.local/services/GetAccountSummary.GetAccountSummaryHttpSoap11Endpoint/");
var binding = new BasicHttpBinding {
Security = {
Mode = BasicHttpSecurityMode.Transport,
Transport = {
ClientCredentialType = HttpClientCredentialType.Basic
}
}
};
//// Create client
GetAccountSummaryPortTypeClient client = new SMSWS.GetAccountSummaryPortTypeClient(binding, endPointAddr);
client.ClientCredentials.UserName.UserName = "XXX";
client.ClientCredentials.UserName.Password = "XXX";
GetAccountSummaryResponse response = null;
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) {
//Create Creds (class is defined below in same Namespace)
BasicAuth auth = new BasicAuth() {
Name = "XXX",
Password = "XXX"
};
//Add HTTP Auth Header (if needed)
//var messageProperty = new HttpRequestMessageProperty();
//messageProperty.Headers.Add(HttpRequestHeader.Authorization, "FOOOOO");
//OperationContext.Current.OutgoingMessageProperties.Add(System.ServiceModel.Channels.HttpRequestMessageProperty.Name, messageProperty);
//Add Auth to SOAP Header
MessageHeader header
= MessageHeader.CreateHeader(
"BasicAuth",
"http://soap-authentication.org/basic/2001/10/",
auth
);
OperationContext.Current.OutgoingMessageHeaders.Add(header);
//Build Request
AccountSearchCriteriaWrapper request = new AccountSearchCriteriaWrapper() {
account = "009364680366"
};
//Send Request
response = client.GetAccountSummary(request);
}
Console.ReadLine();
}
}
public class BasicAuth {
public string Name { get; set; }
public string Password { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment