Last active
December 14, 2015 08:19
-
-
Save adilakhter/5057151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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