Skip to content

Instantly share code, notes, and snippets.

@adilakhter
Last active Dec 14, 2015
Embed
What would you like to do?
public class MessageSecurityBehavior:IEndpointBehavior
{
public ProtectionLevel ProtectionLevel { get; set; }
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
//Do nothing
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
//Do nothing
}
public void Validate(ServiceEndpoint endpoint)
{
//Do nothing
}
public void AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection parameters)
{
//Setting the ProtectionLevel at the Service Contract
serviceEndpoint.Contract.ProtectionLevel = ProtectionLevel;
parameters.Remove<channelprotectionrequirements>();
ChannelProtectionRequirements requirements = new ChannelProtectionRequirements();
parameters.Add(requirements);
MessagePartSpecification unprotectedBody = new MessagePartSpecification();
MessagePartSpecification protectedBody = new MessagePartSpecification(true);
switch (ProtectionLevel)
{
case ProtectionLevel.None:
requirements.OutgoingSignatureParts.AddParts(unprotectedBody, "*");
requirements.IncomingSignatureParts.AddParts(unprotectedBody, "*");
requirements.OutgoingEncryptionParts.AddParts(unprotectedBody, "*");
requirements.IncomingEncryptionParts.AddParts(unprotectedBody, "*");
break;
case ProtectionLevel.Sign:
requirements.OutgoingSignatureParts.AddParts(protectedBody, "*");
requirements.IncomingSignatureParts.AddParts(protectedBody, "*");
requirements.OutgoingEncryptionParts.AddParts(unprotectedBody, "*");
requirements.IncomingEncryptionParts.AddParts(unprotectedBody, "*");
break;
case ProtectionLevel.EncryptAndSign:
requirements.OutgoingSignatureParts.AddParts(protectedBody, "*");
requirements.IncomingSignatureParts.AddParts(protectedBody, "*");
requirements.OutgoingEncryptionParts.AddParts(protectedBody, "*");
requirements.IncomingEncryptionParts.AddParts(protectedBody, "*");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment