first we need to implement Behavior and inspector in the client side to send Key to authenticate the client :
class AuthenticationBehaviour : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
//AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
//endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
}
public void Validate(ServiceEndpoint endpoint)
{
}
class AuthenticationMessageInspector : IClientMessageInspector
{
private const string HeaderKey = "Authentication";
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
if (Session.MachineId == 0)
{
Session.MachineId = LicenseGenerator.GenerateLicense();
}
request.Headers.Add(MessageHeader.CreateHeader(HeaderKey, string.Empty, Session.MachineId));
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
now we need to implement Behavior and inspector in the server side (WCF service) to inspect every request made and extract the header then validate it :
public class AuthenticationBehaviour : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
//AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
//clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
//Console.WriteLine("Dispatcher Applied!");
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
public class AuthenticationMessageInspector : IDispatchMessageInspector
{
#region Members
private string conStr = "", commStr = "";
public IDbConnection Connection { get; set; }
public IDbCommand Command { get; set; }
public IDataReader Reader { get; set; }
#endregion
private const string HeaderKey = "Authentication";
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
//Console.WriteLine("recieved Request! ");
int headerIndex = request.Headers.FindHeader(HeaderKey, string.Empty);
if (headerIndex < 0 || string.IsNullOrEmpty(request.Headers.GetHeader<String>(headerIndex)))
{
throw (new Exception("Access Denied!\n"));
return null;
}
bool valid = Validate(request.Headers.GetHeader<String>(headerIndex));
if (!valid)
{
Console.WriteLine("recieved Request! From " + request.Headers.GetHeader<String>(headerIndex) + " and Access Denied!\n");
throw (new Exception("Access Denied!\n" + request.Headers.GetHeader<String>(headerIndex) + " License Number is not athourized! "));
}
if (headerIndex != -1)
{
Console.WriteLine("recieved Request! From " + request.Headers.GetHeader<String>(headerIndex));
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
now lets register the behavior :
_routingHost.Description.Endpoints[0].Behaviors.Add(new Gateway.Controllers.AuthenticationBehaviour());
_routingHost.Open();
that is it thanks.