Skip to content

Instantly share code, notes, and snippets.

@yaronn
yaronn / encoder.cs
Created January 27, 2014 17:50
messgae encoder that changes the message
public class ReplacePrefixMessageEncodingBindingElement : MessageEncodingBindingElement
{
MessageEncodingBindingElement inner;
Dictionary<string, string> namespaceToPrefixMapping = new Dictionary<string, string>();
public ReplacePrefixMessageEncodingBindingElement(MessageEncodingBindingElement inner)
{
this.inner = inner;
}
private ReplacePrefixMessageEncodingBindingElement(ReplacePrefixMessageEncodingBindingElement other)
@yaronn
yaronn / X509SecurityTokenParameters.cs
Last active January 4, 2016 00:09
X509SecurityTokenParameters
X509SecurityTokenParameters x509Params = new X509SecurityTokenParameters();
x509Params.X509ReferenceStyle =
X509KeyIdentifierClauseType.SubjectKeyIdentifier;
x509Params.RequireDerivedKeys = false;
;
x509Params.InclusionMode = SecurityTokenInclusionMode.Never;
x509Params.ReferenceStyle = SecurityTokenReferenceStyle.Internal;
x509Params.X509ReferenceStyle = X509KeyIdentifierClauseType.Any;
((AsymmetricSecurityBindingElement) sec).InitiatorTokenParameters = x509Params;
public static void SignXmlFile(string FileName, string SignedFileName, RSA Key)
{
// Create a new XML document.
XmlDocument doc = new XmlDocument();
// Load the passed XML file using its name.
doc.Load(new XmlTextReader(FileName));
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(doc);
var b = new CustomBinding();
var sec = (AsymmetricSecurityBindingElement)SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10);
sec.EndpointSupportingTokenParameters.Signed.Add(new UserNameSecurityTokenParameters());
sec.MessageSecurityVersion =MessageSecurityVersion.
WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;
sec.IncludeTimestamp = true;
sec.MessageProtectionOrder = System.ServiceModel.Security.MessageProtectionOrder.SignBeforeEncrypt;
sec.EnableUnsecuredResponse = true;
b.Elements.Add(sec);
using System.Net.Security;
[ServiceContract(..., ProtectionLevel=ProtectionLevel.Sign)]
public interface ServicePortType {...}
var b = new CustomBinding();
var sec = (AsymmetricSecurityBindingElement)SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10);
sec.EndpointSupportingTokenParameters.Signed.Add(new UserNameSecurityTokenParameters());
sec.MessageSecurityVersion =
MessageSecurityVersion.
WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;
sec.IncludeTimestamp = false;
sec.MessageProtectionOrder = System.ServiceModel.Security.MessageProtectionOrder.EncryptBeforeSign;
@yaronn
yaronn / admin
Created November 29, 2013 18:25
rabbitmq
#rabitmq is already installed in ubuntu
#start/stop/status
$> sudo rabbitmqctl status
OR
$> sudo invoke-rc.d rabbitmq-server status
#enable mgmt console
$> /usr/lib/rabbitmq/lib/rabbitmq_server-2.7.1/sbin/rabbitmq-plugins enable rabbitmq_management
namespace ConsoleApplication309
{
public class B : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
MessagePartSpecification m = new MessagePartSpecification();
//see full information and possible error messages here:
http://webservices20.blogspot.co.il/2012/06/12-common-wcf-interop-confusions.html
//This is the most common WCF security setting for interoperability
<customBinding>
<binding name="NewBinding0">
<textMessageEncoding messageVersion="Soap11" />
<security authenticationMode="MutualCertificate" includeTimestamp="false"
messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
<secureConversationBootstrap />
@yaronn
yaronn / decrypt
Last active January 24, 2024 00:19
Decrypt SOAP Body encrypted with X.509 certificate with WS-Security (C#).
//decrypt the encryptedKey to get the session key:
//==================================================
string wrappingKey = "put here the value base64 CipherValue under the encryptedKey element";
X509Certificate2 serverCert = new X509Certificate2(File.ReadAllBytes(@"c:\temp\xws-security-server.p12"), "changeit");
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)serverCert.PrivateKey;
var enckey = rsa.Decrypt(Convert.FromBase64String(wrappingKey), true);
return enckey;
//decrypt the soap body using the session key (aes128):